【问题标题】:JQuery and Ajax - Coming to GripsJQuery 和 Ajax - 即将掌握
【发布时间】:2011-01-20 18:23:20
【问题描述】:

我已经阅读了网络上关于使用 JQuery 进行 ajax 发布的几个教程,它们都将来自网络服务的响应对象引用为 response / response.d -- 这让我相信这是内置对象用于 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:未定义响应”。我做了哪些假设需要重新评估。

【问题讨论】:

    标签: asp.net jquery ajax web-services


    【解决方案1】:

    你需要成功提供一个函数来执行:

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

    【讨论】:

      【解决方案2】:

      成功(如失败)需要一个函数来传递响应对象。

      $('.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);
              }
          });
      });
      

      【讨论】:

      • pedantic response 为了让它验证,回复代码需要一个 , 在 Success 函数 {} 的右括号之后,
      猜你喜欢
      • 2011-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-27
      相关资源
      最近更新 更多