2012-11-13 90 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/