【问题标题】:How to consume the return value of a webservice in jquery ajax call?如何在 jquery ajax 调用中使用 web 服务的返回值?
【发布时间】:2011-10-15 11:27:48
【问题描述】:

我在 asp.net 中有一个简单的 web 服务,它可以确定输入参数是否有效:

[WebMethod]
public bool IsValidNationalCode(string input)
{
    return input.IsNationalCode();
}

我通过 jquery ajax 函数从一个 aspx 页面调用它:

 $('#txtNationalCode').focusout(function () {
      var webMethod = "../PMWebService.asmx/IsValidNationalCode";
      var param = $('#txtNationalCode').val();
      var parameters = "{input:" + param + "}";

      $.ajax({
          type: "POST",
          url: webMethod,
          data: parameters,
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function (msg) {
              if(msg.responseText == true)
                  $('#status').html("Valid");
              else {
                  $('#status').html("Invalid");
              }
          },
          error: function () {
              $('#status').html("error occured");
          }
     });
 });

但我不知道如何获取 webservice 的返回值以显示适当的消息。这里if(msg.responseText == true) 不起作用

【问题讨论】:

    标签: asp.net ajax web-services jquery


    【解决方案1】:

    将 IsValidNationalCode 方法设为静态并在 javascript 中使用:

    success: function (msg) {
        if (msg.d == true)
            $('#status').html("Valid");
        else {
            $('#status').html("Invalid");
        }
    }
    

    对于“d”的解释请点击此链接:Never worry about ASP.NET AJAX’s .d again

    【讨论】:

    • 谢谢,如果返回类型是 html 而不是 json,msg.d 就可以工作。我将 returntype 转换为 'html' 并且由 msg.d 工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-07
    • 2017-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多