-2

我如何获得关于成功、失败等的各种 ajax 消息?我想向用户提醒成功和错误值。这是我下面的代码

我认为 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
            }
        });

    });
}); 
4

2 回答 2

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/

于 2012-11-13T22:40:04.967 回答
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

于 2012-11-13T22:42:02.527 回答