[英]How does one utilize JSON response data from an AJAX call?
I can't seem to execute any code from an AJAX call or utilize the JSON response. 我似乎无法从AJAX调用执行任何代码,也无法利用JSON响应。 What am I missing? 我想念什么? The call is successful, and I can see in Firebug that the script executes fine on the server side, but I can't seem to do anything with the data that comes back. 调用成功,并且在Firebug中可以看到脚本在服务器端可以很好地执行,但是我似乎无法对返回的数据进行任何处理。
review.single = {
id: null,
ip: null,
el: null,
addFlag: function(){
if(this.id && foo.user_id){
$.ajax({
type: 'post',
url: '/reviews/add_flag/',
data: ,
dataType: 'json',
success: function(data){
if(data.success){
this.el.text('Flagged');
this.el.css({
'text-indent': none,
backgroundColor: '#c00',
color: '#fff'
})
} else {
$('#growl').text(data.message);
$('#growl').fadeIn(500).delay(5000).fadeOut(200);
}
}
})
}
}
}
I get this back on one of the calls response types: 我在一种呼叫响应类型上得到了回复:
{success: false, message: "You have already flagged this item."}
Ideally, I'd like to be able to take the message and throw it into the growl and show it, and use the success
property as a check. 理想情况下,我希望能够接收消息并将其放入咆哮中并显示出来,并使用success
属性作为检查。
UPDATE: 更新:
I added an error
block; 我添加了一个error
块; looks like this: 看起来像这样:
error: function (request, status, error) {
alert(request.responseText);
}
As a result, I get an alert that says: 结果,我收到一条警告:
{success: false, message: "You have already flagged this item."}
Figured it out. 弄清楚了。
My JSON was apparently not formatted correctly, and the properties needed to be wrapped in "
. This threw an error back to jQuery. Thanks for the help, guys. 我的JSON显然格式不正确,并且属性需要用"
包裹"
。这将错误返回给jQuery。感谢您的帮助。
Example: 例:
{"success": false, "message": "You have already flagged this item."}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.