开发者

JQuery JSON parsing / AJAX issue ('remove' and '__proto__' keys in arrays?)

开发者 https://www.devze.com 2023-02-15 01:35 出处:网络
I am seeing a strange issue when fetchin开发者_如何学编程g a JSON object from a URL using Jquery $.ajax function.

I am seeing a strange issue when fetchin开发者_如何学编程g a JSON object from a URL using Jquery $.ajax function.

I am using the following JQuery call to retrieve the JSON object:

1  $.ajax({
2       url: '/test/getjson',
3       success: function(data){
4                     doSomething(data); //(breakpoint here)
5                },
6       error: function(x,y,z){
7                     //error..
8               }
9  });

The URL '/test/getjson' returns the following JSON object (Here is what the response looks like in Firebug):

{
    "rsp": {
        "date": "1299195954782" ,
        "type": "Type1" ,
        "Main": {
            "Category1" : {
                "private" : "Y" ,
                "properties" : {
                    "one" : {
                        "response" : "" ,
                        "text" : "label" ,
                        "type" : "property"
                    },
                    "two" : {
                        "options" : [
                            "1" ,
                            "2" ,
                            "3" ,
                            "4" ,
                            "5" ,
                            "6" ,
                            "7" ,
                            "8" ,
                            "9" ,
                            "10"
                        ],
                        "response" : "1" ,
                        "text" : "label2" ,
                        "type" : "property2"
                    }
                }
            },
            "username" : "spiderman"
        }
    }
}

Problem

The problem is that all arrays in the JSON object have extra values with keys 'remove' and '__proto__' after being parsed by JQuery (Firebug's Debugger viewing JSON data object, breakpoint at line 4 in the JS snippet above):

JQuery JSON parsing / AJAX issue ('remove' and '__proto__' keys in arrays?)

[JSON object as seen by Firebugs debugger]

And here is a closer look at the strange part of the JSON object:

JQuery JSON parsing / AJAX issue ('remove' and '__proto__' keys in arrays?)

[Closer look at the unknown data]

Thanks ahead everyone :)


Stop using "for ... in" to iterate over arrays, and use an index variable.

for (var i = 0; i < theArray.length; ++i) {
  var element = theArray[i];
  // ...
}

Those object properties are there thanks to the JavaScript runtime in your browser. I'm not sure why they'd cause you any trouble because they should not be iterable. Perhaps if you showed the actual code that you've got to process the ajax response, the issue might become more clear.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号