开发者

$.ajax parsing issue (jQuery)

So I have an array:

[{'key1':'a', 'key2':'b', 'ProblemKey': {'keyP1': 'c', 'KeyP2':'d'}}, {'key1':'e', 'key2':开发者_如何学Python 'f', 'ProblemKey': ....}}]

When I do the standard $.each loop through the received data (above) from my GET response, all of the keys correspond correctly to their value for each object in the array except the "problemKey"s (because their values are associative arrays and not strings?). Those always come back as Undefined. Is there any way to get the $.ajax method to parse these parts correctly? Or should I return the data as a text document and get some third party plugin that has better parsing abilities than the one that comes with jQuery already?


$.ajax({
            url:'Your post url',
            data : ({
                'elem':elemtopost,

            }),
            method : 'POST',
            dataType: 'json',                 
            success: function(msg){ 

                    for(j=0;j<msg.length;j++){
                            alert(msg[j]['key1']  //accessing the json string
                    }
});


In your $.ajax call, set the dataType to "json":

$.ajax({
   // other stuff
   dataType: "json"
});

Then in the success function you can access the return value with the dot operator:

var myVal = returnArray[0].ProblemKey.keyP1;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜