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

Christian Strempfer
  • 7,291
  • 6
  • 50
  • 75
Johnston
  • 2,873
  • 8
  • 29
  • 39
  • 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. – Sergey Akopov Apr 07 '12 at 02:52
  • 1
    I'll admit I'm not familiar with the server side of this, but does this post help? http://stackoverflow.com/questions/3191317/passing-array-to-webmethod-using-ajax – dfreeman Apr 07 '12 at 03:06
  • @dfreeman it does thanks, It lead me to another question in that post which had exactly what i needed :) – Johnston Apr 07 '12 at 03:32
  • possible duplicate of [Passing array of strings to webmethod with variable number of arguments using jQuery AJAX](http://stackoverflow.com/questions/7971393/passing-array-of-strings-to-webmethod-with-variable-number-of-arguments-using-jq) – weir Jun 18 '14 at 17:15

1 Answers1

0

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

Johnston
  • 2,873
  • 8
  • 29
  • 39