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?
}
});
};
First, make sure you’ve tagged your class with
[ScriptService]to allow it to be called through AJAX. Something like:You can then read the result with jQuery directly, as there’s no need to parse anything:
Another approach is to just include a reference to:
This will generate proxy classes to allow you to call web methods directly. For example:
The
onCompletefunction 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.