这是一个非常奇怪的情况。我正在遍历一些div来收集要发送到后端PHP的信息,但是其中一个值永远不会传到后端。
这是我的前端代码:
$(".allDivs").each(function(){
var symbol;
var currentId = $(this).attr("id");
var currentId = currentId.replace("div", "");
var originalSymbol = $.trim($("#symbol" + currentId).val());
var offerPrice = $.trim($("#offerPrice" + currentId).val());
if (originalSymbol.length == 5)
{
symbol = originalSymbol.slice(0,-1);
}
else
{
symbol = originalSymbol;
}
symbolArray.push({
"symbol": symbol,
"originalSymbol" : originalSymbol,
"offerPrice" : offerPrice,
"checkNews" : "TEST",
"idNumber": currentId,
});
});
如果我是在调用AJAX之前调试对象的,则外观如下:
console.log(JSON.stringify(symbolArray));
产生:
[{"symbol":"MSFT","originalSymbol":"MSFT","offerPrice":"","checkNews":"TEST","idNumber":"1"}]
因此您可以看到checkNews的值为“ TEST”
这是AJAX的样子:
$.ajax({
url: "newsproxy.php",
data: {symbols: JSON.stringify(symbolArray)},
async: true,
dataType: 'json',
success: function (data) {
// etc...
如果我尝试在后端打印一些信息,则如下所示:
if(isset($_GET['symbols']))
{
$symbols=$_GET['symbols'];
}
foreach ($symbols as $symbol)
{
$symbol = $symbol->symbol;
$checkNews = $symbol->checkNews;
echo "symbol is *" . $symbol . "*" . " and checkNews is *" . $checkNews . "*";
}
这将产生以下内容:
symbol is *MSFT* and checkNews is **
为什么我的符号变量会进入后端,但checkNews变量被取消?