1

I am currently trying to pass an array that I have created in Javascript to my webmethod in my aspx.cs.

Heres what I have:

JAVASCRIPT

function callServer(requestMethod, clientRequest) {


    var pageMethod = "Default.aspx/" + requestMethod;
    $.ajax({
        url: pageMethod,   // Current Page, Method  
        data: JSON.stringify({ request: clientRequest }), // parameter map as JSON  
        type: "POST", // data has to be POSTed  
        contentType: "application/json", // posting JSON content      
        dataType: "JSON",  // type of data is JSON (must be upper case!)  
        timeout: 60000,    // AJAX timeout  
        success: function (result) {
            ajaxCallback(result.d);
        },
        error: function (xhr, status) {
            alert(status + " - " + xhr.responseText);
        }
    });
}



function myButtonCalls()
{
var values=[];
values[0] = "Hello";
values[1] = "goodbye";

callServer("myMethod", values);
}

ASPX.CS

 [WebMethod]
        public static string myMethod(string[] request)
        {
return "This is test";
    }

It fails before it even gets to my web method. I know this code works for regualr strings but The ajax code that uses JSON doesnt see to want to work with arrays.

Any ideas of what i need to change?

Thanks

4
  • Try debuging your ajax request with Developer Tools in chrome or Firefox. Either one should tell you what response you're getting from the server. Commented Apr 7, 2012 at 2:52
  • 1
    I'll admit I'm not familiar with the server side of this, but does this post help? stackoverflow.com/questions/3191317/… Commented Apr 7, 2012 at 3:06
  • @dfreeman it does thanks, It lead me to another question in that post which had exactly what i needed :) Commented Apr 7, 2012 at 3:32
  • possible duplicate of Passing array of strings to webmethod with variable number of arguments using jQuery AJAX Commented Jun 18, 2014 at 17:15

1 Answer 1

0

In the aspx.cs, I needed to accept with type list not array. Thanks for the comments!

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.