IE中的Javascript/PHP JSON数组为空
嗨,我有一个javascript代码,通过ajax连接到php脚本。此php脚本返回一个数组。在ajax调用的success函数中,我使用返回的数组向用户显示信息。这一切在我尝试过的所有浏览器中都很好,除了InternetExplorer。我得到以下错误:IE中的Javascript/PHP JSON数组为空,javascript,php,ajax,json,Javascript,Php,Ajax,Json,嗨,我有一个javascript代码,通过ajax连接到php脚本。此php脚本返回一个数组。在ajax调用的success函数中,我使用返回的数组向用户显示信息。这一切在我尝试过的所有浏览器中都很好,除了InternetExplorer。我得到以下错误: Unable to get property '0' of undefined or null reference “0”是数组中第一个元素的索引。代码如下: JS PHP 我在success函数中尝试了console.log(data),
Unable to get property '0' of undefined or null reference
“0”是数组中第一个元素的索引。代码如下:
JS
PHP
我在success函数中尝试了console.log(data)
,在InternetExplorer中它返回null,而在其他浏览器中它返回数组。有人知道这里出了什么问题吗
编辑:IE中控制台上的错误代码是SCRIPT5007。搜索时,这意味着:
You attempted to invoke the Object.prototype.toString or Object.prototype.valueOf method on an object of a type other than Object. The object of this type of invocation must be of type Object.
链接:尝试:
$output = array();
$output[] = $itemname;
$output[] = $itemdescription;
$output[] = $itemprice;
echo json_encode($output);
exit();
请注意,$output[]=xxx
表示:将xxx追加到$output
中,$output[1]=xxx
表示将xxx置于$output
的索引1中
请注意,您也可以这样做:
$output = array($itemname, $itemdescription, $itemprice);
编辑注释和多个操作编辑:
尝试打印数据以查看发生了什么:
success: function (data) {
console.log(data);
document.getElementById("name").innerHTML = data[0];
document.getElementById("desc").innerHTML = data[1];
document.getElementById("price").innerHTML = data[2];
},
您将在控制台(使用F12)中看到
数据
包含的内容不太清楚为什么要尝试将其扭曲为kv对象
$output = array(
'item' => array (
$itemname, $itemdescription, $itemprice
)
);
echo json_encode($output);
exit();
查看生成的输出抱歉,有一个错误在我的问题中再次查看代码块。我正在添加3个不同的索引,您需要设置索引0,这在javascript中使用。因此,您将设置
$output[0]=$itemname$输出[1]=$itemdescription$输出[2]=$itemprice请注意,数组的第一个索引是0,而不是1。由于变量包含长字符串,我不想像您建议的那样在一行中完成。我不认为我的PHP代码有问题,如果所有其他浏览器都能很好地执行它,正如我在OP中所说的,我已经尝试了console.log(data)并在IE中返回null,但是其他浏览器它返回了数组,console.log()说如果删除数据类型:json
行,会发生什么?
success: function (data) {
console.log(data);
document.getElementById("name").innerHTML = data[0];
document.getElementById("desc").innerHTML = data[1];
document.getElementById("price").innerHTML = data[2];
},
$output = array(
'item' => array (
$itemname, $itemdescription, $itemprice
)
);
echo json_encode($output);
exit();