最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论