簡體   English   中英

Ajax-返回的json對象在ie中為null

[英]Ajax - returned json object is null in ie

嗨,我有一個JavaScript代碼,可通過ajax連接到php腳本。 這個PHP腳本返回一個數組。 在ajax調用的成功函數中,我使用返回的數組向用戶顯示信息。 在我嘗試使用的所有瀏覽器(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)

我無法用您的示例代碼重現該問題。 我已經在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>

筆記:
-由於示例“ add.php”文件的位置,我在Ajax URL中使用“ ./”。
-我使用對象而不是字符串作為數據。 這就是我通常構建數據變量的方式。
-除了成功以外,我嘗試使用$ .done仍然能夠檢索數據。

輸出:

名稱
項目描述:1
價錢

嘗試使用$ .done方法,看看是否有幫助。 https://api.jquery.com/deferred.done/

我還建議在開發人員工具中監視網絡以驗證請求和響應。

來源: 如何使用jQuery AJAX和PHP數組返回

該鏈接提到在返回的數據上運行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);
}

});

只是一個想法,對不起,我現在無法測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM