2012-11-13 65 views
-2

我如何获得成功,失败等各种阿贾克斯消息?我想提醒成功和错误值给用户。这是我下面如何获取responseText消息?

代码,我觉得200是成功的..我如何访问成功和失败值,这样我就可以基于该

$(function() { 
    $("#save").click(function() { 

     $.ajax({ 
      type: "POST", 
      url: "some.json", 
      data: json_data, 
      contentType: 'application/json', 
      success: function() { 

       //if success forward to landing page else return to form for correctiion 
      } 
     }); 

    }); 
}); 
+0

这消息你在说什么? – Magus

+1

为什么不阅读[文档](http://api.jquery.com/jQuery.ajax/)? '成功(data,textStatus,jqXHR)' – undefined

+1

也看看这里:http://api.jquery.com/jQuery.ajax/ –

回答

0
$(function() { 
       $("#save").click(function() { 


        $.ajax({ 
         type: "POST", 
          url: "some.json", 
          data: json_data, 
          contentType : 'application/json', 
          success: function(data, textStatus, jqXHR) { 
          // get success or failure from here 
         alert(jqXHR.responseText) 
          } 
         }); 

        }); 
       }); 

更多的选项jqXHR onject:http://api.jquery.com/jQuery.ajax/#jqXHR

+0

什么是textStatus? textStatus的值是从哪里来的? – user244394

+0

@ user244394 jQuery内部调用您的成功处理程序与数据,textStatus,jqXHR ..请阅读其他人和我发布的链接。 –

0

实例提醒他们,做一些事情:发送一个id为数据发送到服务器,将一些数据保存到服务器,并在用户完成后通知用户。如果请求失败,请提醒用户。

var menuId = $("ul.nav").first().attr("id"); 
var request = $.ajax({ 
    url: "script.php", 
    type: "POST", 
    data: {id : menuId}, 
    dataType: "html" 
}); 

request.done(function(msg) { 
    $("#log").html(msg); 
}); 

request.fail(function(jqXHR, textStatus) { 
    alert("Request failed: " + textStatus); 
}); 

链接:http://api.jquery.com/jQuery.ajax/