How to access data in JSON response recieved from JQuery?

This question already has an answer here:

  • Parse JSON in JavaScript? [duplicate] 16 answers

  • If you add dataType: "json" to the call the response will be made a json object:

    $.ajax({
        url: 'buildings.php',
        data: "building=" + building,
        dataType: "json",
        complete: function (response) {
            alert(response.name);
        }
    });
    

    Edit: so it appears that for whatever reason jQuery wasn't able to parse it automatically, but JSON.parse(response.responseText) did the trick.


    你可以jQuery.getJSON()并检查响应的contentType


    Is your PHP script returning the correct MIME type in the headers? As shown here - Returning JSON from a PHP Script

    If so, then add this to the options.

    dataType: "json",
    

    One of the easiest mistakes to make if your content header is right is that of returning a quoted string instead of the actual JSON. ie. the actual return contents being

    "{ "key": "value" }"
    

    instead of

    { "key": "value" }
    
    链接地址: http://www.djcxy.com/p/8496.html

    上一篇: 如何在JavaScript中解析漂亮的JSON

    下一篇: 如何从JQuery中接收JSON响应中的数据?