Ajax - 返回的json对象在ie中为null

时间:2014-03-15 18:47:15

标签: javascript php jquery ajax json

您好我有一个通过ajax连接到php脚本的JavaScript代码。这个php脚本返回一个数组。在ajax调用的success函数中,我使用返回的数组向用户显示信息。这一切在我尝试过的所有浏览器中都能正常工作,除了Internet Explorer。我收到以下错误:

Unable to get property '0' of undefined or null reference

'0'是数组中第一个元素的索引。这是代码:

JS

$.ajax({
    type: "POST",
    url: "/add.php",
    data: 'id=' + itemid,
    dataType: "json",
    success: function (data) {
        document.getElementById("name").innerHTML = data[0];
        document.getElementById("desc").innerHTML = data[1];
        document.getElementById("price").innerHTML = data[2];
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
    }
});

PHP

$output = array();
$output[0] = $itemname;
$output[1] = $itemdescription;
$output[2] = $itemprice;
echo json_encode($output);
exit();

我在成功函数中尝试了console.log(data),在Internet Explorer中它返回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.

链接:http://msdn.microsoft.com/query/dev12.query?appId=Dev12IDEF1&l=EN-GB&k=k(VS.WebClient.Help.SCRIPT5007)

2 个答案:

答案 0 :(得分:1)

我无法使用您的示例代码重现该问题。我已经在Safari和IE 11上测试了你的代码。

以下是我使用的示例代码(由您修改):

PHP代码示例

<?php
$output = array();
$output[0] = 'Name';
$output[1] = 'Description for Item: ' . $_POST['id'];
$output[2] = 'Price';
echo json_encode($output);
exit();
?>

由于我不知道$ itemname,$ itemdescription或$ itemprice是什么,所以除了传入的id之外,我硬编码了值。

HTML代码示例

<html>
    <head>
        <title></title>
    </head>
    <body>
        <div id="name"></div>
        <div id="desc"></div>
        <div id="price"></div>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script>
            var $Xhr = $.ajax({
                type: "POST",
                url: "./add.php",
                data: {
                    id: 1
                },
                dataType: "json",
                success: function () {},
                error: function (xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
                }
            });
            $Xhr.done(function renderData(data){
                document.getElementById("name").innerHTML = data[0];
                document.getElementById("desc").innerHTML = data[1];
                document.getElementById("price").innerHTML = data[2];
            });
        </script>
    </body>
</html>

注意:
  - 我用&#39; ./'在Ajax URL中,由于我的样本的位置&quot; add.php&#39;文件。
  - 我使用了一个对象来代替字符串。这就是我通常构建数据变量的方式   - 成功的替代方案,我尝试使用$ .done并且仍然能够检索数据。

<强>输出:

  

名称
项目描述:1
价格

尝试使用$ .done方法,看看这是否对您有所帮助。 https://api.jquery.com/deferred.done/

我还建议在开发人员工具中监控网络以验证请求和响应。

答案 1 :(得分:0)

来源:How to work with jQuery AJAX and PHP array return

此链接提到在返回的数据上运行eval以将其转换为对象。 所以,也许试试:

$.ajax({
type: "POST",
url: "/add.php",
data: 'id=' + itemid,
dataType: "json",
success: function (data) {
    var success_data = eval (data);
    document.getElementById("name").innerHTML = success_data[0];
    document.getElementById("desc").innerHTML = success_data[1];
    document.getElementById("price").innerHTML = success_data[2];
},
error: function (xhr, ajaxOptions, thrownError) {
    alert(xhr.status);
    alert(thrownError);
}

});

只是一个想法,抱歉,我现在无法对此进行测试。