开发者

How to consume the return value of a webservice in jquery ajax call?

开发者 https://www.devze.com 2023-04-13 09:51 出处:网络
I have a simple webservice in asp.net which determine if the input parameter is valid or not : [WebMethod]

I have a simple webservice in asp.net which determine if the input parameter is valid or not :

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

I call it from an aspx page by jquery ajax function :

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

But I don't know how to get the return value of webservice in order to show appropriate message . Here if(msg.responseText == true) doesn't work


Make the IsValidNationalCode method static and use this in javascript:

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

For "d" explanation follow this link: Never worry about ASP.NET AJAX’s .d again

0

精彩评论

暂无评论...
验证码 换一张
取 消