Javascript 使用JSON响应
在执行ajax请求后,我从服务器获得以下响应:Javascript 使用JSON响应,javascript,jquery,ajax,json,Javascript,Jquery,Ajax,Json,在执行ajax请求后,我从服务器获得以下响应: {"error":false,"success":true} 我的ajax代码: $.ajax({ url: '/update', type: 'post', data: $(this).serialize(), success: function(response) { alert(response) }, error: function() { alert('An
{"error":false,"success":true}
我的ajax代码:
$.ajax({
url: '/update',
type: 'post',
data: $(this).serialize(),
success: function(response) {
alert(response)
},
error: function() {
alert('An error occured, form not submitted.');
}
});
我不想提醒整个响应,而是想提醒“success”的值,在本例中,这是正确的。如何做到这一点?试试这个:
alert(response.success);
alert(response.success);
像这样:
要做到这一点,您可以将
数据类型:“json”
添加到$.ajax选项中,以确保它在回调中作为对象进行计算。您必须将json解析为JavaScript对象:@genesis--是的,$.ajax()
通常知道返回基于响应的类型,案例内容类型为application/json+谢谢,添加数据类型就可以了。我在发布问题之前就调用了它,但数据类型是我所必需的。
$.ajax({
url: '/update',
type: 'post',
dataType: 'json',
data: $(this).serialize(),
success: function(response) {
alert(response.success)
},
error: function() {
alert('An error occured, form not submitted.');
}
});
alert(response.success);