1

I am working on a project and have to send a JavaScript Array as a parameter of a ASP.Net function which parameter is ArrayList.

Below is my code,

JavaScript :

        var propertyArray = new Array();
        propertyArray.push("2");
        propertyArray.push("3");

        $.ajax({
            type: 'POST',
            url: 'Default.aspx/SaveTextProperty',
            contentType: 'application/json; charset=utf-8',
            data: { propertyArray: propertyArray },
            dataType: 'json',
            success: function (response) {
                var result = "Done";
                alert(result);
            }
        });

Default.aspx :

    [WebMethod]
    public static bool SaveTextProperty(ArrayList propertyArray)
    {
          //Some code
          return true;
    }

Here I need to get JavaScript propertyArray as ASP.Net function named SaveTextProperty's parameter. How can I get it? Thank you.

1 Answer 1

1

You can use as follow

[WebMethod]
public static bool SaveTextProperty(List<string> arr)
{
      //Some code
      return true;
}

and jquery

var propertyArray = new Array();
    propertyArray.push("2");
    propertyArray.push("3");

    $.ajax({
        type: 'POST',
        url: 'Default.aspx/SaveTextProperty',
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify({ arr: propertyArray }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: onSuccess,
        failure: onError
    });

function onSuccess(response) {
    alert(response.d);
}

function onError() {
    alert("fail");
}
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.