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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.