1

I have a WebMethod which returns a string of array:

[WebMethod]
public static string[] GetDataFromServer()
{
    return new string[] { "one", "two", "three" };
}

I am calling the WebMethod using the following code:

$.ajax({
    type: "POST",
    url: "MyPage.aspx/GetDataFromServer",
    data: "{}",
    success: function (msg) {
        alert(msg.d);
    },
    error: function (x, e) {
        alert("The call to the server side failed. " + x.responseText);
    }
});

Since the WebMethod is returning an array of string, when calling alert(msg.d);, I am getting all the elements of the array seperated by a ,. I understand that I can split the msg.d by the , seperator but I don't think that it is good practice. How can I access the different elements in the resulting array by index?

1 Answer 1

1

You should just be able to use msg.d[0], for example to get the first item in the array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.