Asp.net WebMethod - return string[] and parse it using JavaScript - Stack Overflow

I need to return an array of string from MyMethod at codebehind. But do I parse it on aspx page using j

I need to return an array of string from MyMethod at codebehind. But do I parse it on aspx page using javascript?

[WebMethod]
public static string[] MyMethod(){
   return new[] {"fdsf", "gfdgdfgf"};
}

..........
function myFunction() {
            $.ajax({ ......
                    success: function (msg) {
                                //how do I parse msg?
                                }
            });
        };

I need to return an array of string from MyMethod at codebehind. But do I parse it on aspx page using javascript?

[WebMethod]
public static string[] MyMethod(){
   return new[] {"fdsf", "gfdgdfgf"};
}

..........
function myFunction() {
            $.ajax({ ......
                    success: function (msg) {
                                //how do I parse msg?
                                }
            });
        };
Share Improve this question asked Jul 13, 2012 at 21:29 user266003user266003
Add a ment  | 

3 Answers 3

Reset to default 3

First, make sure you've tagged your class with [ScriptService] to allow it to be called through AJAX. Something like:

[ScriptService] //<-- Important
public class WebService : System.Web.Services.WebService
{
   [ScriptMethod] //<-- WebMethod is fine here too
   public string[] MyMethod()
   {
      return new[] {"fdsf", "gfdgdfgf"};
   }
}

You can then read the result with jQuery directly, as there's no need to parse anything:

$(document).ready(function() {
  $.ajax({
    type: "POST",
    url: "WebService.asmx/MyMethod",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
      // msg.d will be your array with 2 strings
    }
  });
});

Another approach is to just include a reference to:

<script src="WebService.asmx/js" type="text/javascript"></script>

This will generate proxy classes to allow you to call web methods directly. For example:

WebService.MyMethod(onComplete, onError);

The onComplete function will receive a single parameter with the results of the web service call, in your case a Javascript array with 2 strings. In my opinion, this is an easier solution than using jQuery and worrying about the URL and HTTP payload.

Use the jQuery iterator to iterate over the strings in the msg result like so.

function myFunction() {
    $.ajax({ ......
        success: function (msg) {
            $.each(msg, function(index, value) {
                alert(value);
            });
        }
    });
};

The response object will contain an object called d which wraps the values returned from your WebMethod. Just access it like so:

function myFunction() {
    $.ajax({ ......
        success: function (msg) {
            //how do I parse msg?
            alert(msg.d); //alerts "fdsf", "gfdgdfgf"
        }
    });
};

See this question for an explanation.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745478765a4629467.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信