2011-01-20 52 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