JSON在Internet Explorer中输出“Undefined”而不是其他浏览器

时间:2012-02-14 07:46:50

标签: php json internet-explorer

----------这是testA.php -------------------------

[{"msgCD":"OK","msgSTR":"Hi"}]

----------这是testB.html -------------------------

$(document).ready(function(){ $('.submit').click(function(){
$.getJSON('testA.php',function(data){
    $.each(data,function(i,n){ 
        $('#message').append(n['msgCD']+'</br>');
    });
});
return false;});});

使用Chrome和Firefox,JSON输出“HI”但IE,它根本不起作用!天啊:(

1 个答案:

答案 0 :(得分:0)

您有一个对象包装在一个数组中,因此您无法直接访问该对象:

var foo = [{"msgCD":"OK","msgSTR":"Hi"}]
foo.msgCD // undefined
foo[0].msgCD // "OK"

编辑: 我还建议你使用chrome控制台,只需创建一个console.log(n),你就可以浏览你的对象了。