2011-09-07 122 views
3

我从服务器做一个Ajax请求后,下面的响应:使用JSON响应

{"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.'); 
    } 
}); 

,而不是提醒整个响应我要提醒在这种情况下,“成功”的价值将是真实的。这个怎么做?

+0

你必须解析JSON到一个JavaScript对象:http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript –

回答

2
alert(response.success); 

将做到这一点,你可以添加dataType: 'json'到您的$就选择绝对确保它的评估作为回调中的一个对象。

+0

谢谢,添加dataType做到了。在发布他的问题之前,我打电话给他,但dataType是我需要的。 – Linda

2

试试这个:

alert(response.success); 
4

像这样:

alert(response.success); 
+0

它不会工作,它不被识别为json默认 – genesis

+2

@genesis - 是的,它会','.ajax()'通常知道什么类型的回报是基于 – Neal

+1

的回应,如果Content-Type是application/json。 +1虽然 – genesis

3
$.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.'); 
     } 
    });