使用JSON响应 [英] Using a JSON response
本文介绍了使用JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从服务器做一个Ajax请求后,下面的响应:
{错误:假的,成功:真}
我的阿贾克斯code:
$。阿贾克斯({
网址:'/更新',
类型:'后',
数据:$(本).serialize()
成功:函数(响应){
警报(响应)
},
错误:函数(){
警报(发生错误,表单没有提交。);
}
});
代替提醒我在此提醒成功,在这种情况下将是真值的整个响应
。如何做到这一点?
解决方案
警报(response.success);
将做到这一点,您可以添加数据类型:JSON
到$阿贾克斯的选择绝对确保它评价为在回调的对象。
I get the following response from the server after doing an ajax request:
{"error":false,"success":true}
My ajax code:
$.ajax({
url: '/update',
type: 'post',
data: $(this).serialize(),
success: function(response) {
alert(response)
},
error: function() {
alert('An error occured, form not submitted.');
}
});
instead of alerting the whole response I want to alert the value of "success", which in this case would be true. How to do this?
解决方案
alert(response.success);
would do it, you can add dataType: 'json'
to your $.ajax options to make absolutely sure it's evaluated as an object in your callback.
这篇关于使用JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文