javascript 如何访问从 JQuery 收到的 JSON 响应中的数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24946498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:37:44  来源:igfitidea点击:

How to access data in JSON response recieved from JQuery?

javascriptjqueryajaxjson

提问by Connorelsea

How do I access the data stored in JSON that is returned in the complete function of a JQuery AJAX request. For example, I have the following code:

如何访问存储在 JSON 中的数据,该数据在 JQuery AJAX 请求的完整函数中返回。例如,我有以下代码:

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

In the first alert it displays the following, which is the intended JSON data that I sent from PHP.

在第一个警报中,它显示以下内容,这是我从 PHP 发送的预期 JSON 数据。

{"name":"HSB","description":"description","directionsURL":"directionsURL","imageArray":{"1":"URL 1","2":"URL 2"}}

In the second alert, it displays

在第二个警报中,它显示

undefined

How do I access the data I received that is displayed in the first alert?

如何访问我收到的显示在第一个警报中的数据?

采纳答案by Jaime Gómez

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

如果添加dataType: "json"到调用中,响应将变成一个 json 对象:

$.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 似乎都无法自动解析它,但它JSON.parse(response.responseText)做到了。

回答by Klors

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

您的 PHP 脚本是否在标头中返回了正确的 MIME 类型?如此处所示 -从 PHP 脚本返回 JSON

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

如果您的内容标头正确,最容易犯的错误之一是返回带引号的字符串而不是实际的 JSON。IE。实际的返回内容是

"{ \"key\": \"value\" }"

instead of

代替

{ "key": "value" }

回答by tuan huynh

You can you jQuery.getJSON()and check the contentType of the response

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

回答by TrazeK

It looks like response.responseTextcontains your JSON packet. Try something like this:

看起来response.responseText包含您的 JSON 数据包。尝试这样的事情:

var json = JSON.parse(response.responseText); //assume response.responseText is a json string
console.log(json.name);

回答by Michael

Your PHP is simply returning a string that lookslike a JSON object, but javascript isn't smart enough to know that you want it to be a JSON object. Just parse the response before you act upon it:

您的 PHP 只是返回一个看起来像 JSON 对象的字符串,但 javascript 不够聪明,无法知道您希望它是一个 JSON 对象。在您采取行动之前,只需解析响应:

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