解析 jQuery AJAX 响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16935711/
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
Parsing jQuery AJAX response
提问by Barry Hamilton
I use the following function to post a form to via jQuery AJAX:
我使用以下函数通过 jQuery AJAX 发布表单:
$('form#add_systemgoal .error').remove();
var formdata = $('form#add_systemgoal').serialize();
$.ajaxSetup({async: false});
$.ajax({
type: "POST",
url: '/admin/systemgoalssystemgoalupdate?format=html',
data: formdata,
success: function (data) {
console.log(data);
},
});
It posts fine but I cannot parse the respons, it logs to console as follows
它发布得很好,但我无法解析响应,它记录到控制台,如下所示
{
"success": 1,
"inserted": {
"goal_id": "67",
"goalsoptions_id": "0",
"user_id": "0",
"value": "dsfdsaf",
"created": "2013-06-05 09:57:38",
"modified": null,
"due": "2013-06-17 00:00:00",
"status": "active",
"actions_total": "0",
"actions_title": "sfdgsfdgdf",
"action_type": "input",
"points_per_action": "1",
"expires": "2013-06-11 00:00:00",
"success": 1
}
}
which I believe is the response I am looking for.
我相信这是我正在寻找的回应。
However when I try to do alert(data.success);
or any of the other members of the response object it is undefined
.
但是,当我尝试做alert(data.success);
或响应对象的任何其他成员时,它是undefined
.
Any advice appreciated.
任何建议表示赞赏。
采纳答案by sjmarshy
calling
打电话
var parsed_data = JSON.parse(data);
should result in the ability to access the data like you want.
应该能够像你想要的那样访问数据。
console.log(parsed_data.success);
should now show '1'
现在应该显示“1”
回答by Kevin Bowersox
$.ajax({
type: "POST",
url: '/admin/systemgoalssystemgoalupdate?format=html',
data: formdata,
success: function (data) {
console.log(data);
},
dataType: "json"
});
回答by RenatoCastanheira
Imagine that this is your Json response
想象一下这是你的 Json 响应
{"Visit":{"VisitId":8,"Description":"visit8"}}
This is how you parse the response and access the values
这是您解析响应并访问值的方式
Ext.Ajax.request({
headers: {
'Content-Type': 'application/json'
},
url: 'api/fullvisit/getfullvisit/' + visitId,
method: 'GET',
dataType: 'json',
success: function (response, request) {
obj = JSON.parse(response.responseText);
alert(obj.Visit.VisitId);
}
});
This will alert the VisitId field
这将提醒 VisitId 字段
回答by Habibillah
you must parse JSON string to become object
您必须解析 JSON 字符串才能成为对象
var dataObject = jQuery.parseJSON(data);
so you can call it like:
所以你可以这样称呼它:
success: function (data) {
var dataObject = jQuery.parseJSON(data);
if (dataObject.success == 1) {
var insertedGoalId = dataObject.inserted.goal_id;
...
...
}
}
回答by Rodik
Since you are using $.ajax
, and not $.getJSON
, your return type is plain text. you need to now convert data
into a JSON object.
由于您使用的是$.ajax
,而不是$.getJSON
,因此您的返回类型是纯文本。您现在需要转换data
为 JSON 对象。
you can either do this by changing your $.ajax
to $.getJSON
(which is a shorthand for $.ajax
, only preconfigured to fetch json).
您可以通过更改您的$.ajax
to $.getJSON
(这是 的简写$.ajax
,仅预配置为获取 json)来完成此操作。
Or you can parse the data
string into JSON after you receive it, like so:
或者您可以data
在收到字符串后将其解析为 JSON,如下所示:
success: function (data) {
var obj = $.parseJSON(data);
console.log(obj);
},