2011-01-20 51 views
2

我已經通過幾個教程關於使用jQuery AJAX張貼在網絡上閱讀,他們都來自web服務引用響應對象響應/ response.d - 這使我相信這是JQuery響應處理程序的內置對象。jQuery和阿賈克斯 - 在着手

代碼段:

$('.submit').click(function() { 
    var theURL = document.location.hostname + ":" + document.location.port + "/LeadHandler.aspx/hello"; // this will change too 
    alert(theURL); 
    $.ajax({ 
     type: "POST", 
     url: theURL, 
     data: "{'NameFirst':'" + $('#txtFirstName').val() + "'}", // again, change this 
     contentType: "applications/json; charset=utf-8", 
     dataType: "json", 
     success: alert("Success: " + response.d), // this will change 
     failure: function (response) { 
      alert("Failure: " + response.d); 
     } 
    }); 
}); 

但是代碼返回:在Chrome的JavaScript控制檯 「未捕獲的ReferenceError響應沒有定義」。我做了什麼假設,我需要重新評估。

回答

6

您需要使用的功能提供成功執行:

success: function(response) { 
    alert(response.d); 
} 
3

成功(失敗一樣)需要一個函數通過傳遞響應對象。

$('.submit').click(function() { 
    var theURL = document.location.hostname + ":" + document.location.port + "/LeadHandler.aspx/hello"; // this will change too 
    alert(theURL); 
    $.ajax({ 
     type: "POST", 
     url: theURL, 
     data: "{'NameFirst':'" + $('#txtFirstName').val() + "'}", // again, change this 
     contentType: "applications/json; charset=utf-8", 
     dataType: "json", 
     success: function (response) { 
      alert("Success: " + response.d); 
     }, 
     failure: function (response) { 
      alert("Failure: " + response.d); 
     } 
    }); 
}); 
+0

*迂迴響應*爲了讓它得到驗證,代碼需要回答,在成功函數{}的右括號後面, – lazyPower 2011-01-20 18:39:15