Reputation: 443
I have a array of string myArray[] in my jQuery the value is generated dynamically. I also have a method in my code behind that has string[] myArray.
I am using the following code in my jQuery
$.ajax({
type: "POST",
url: "myurl/MyMethod",
data: "{'strArray' :" + myArray + ", 'id':" + x + "}",
dataType: "json",
success: function(response){
// DO SOME STUFF WITH THE RESPONSE...
}
});
I have the following in my code behind
[WebMethod]
public static bool MyMethod(string[] strArray, int id)
{
// DO SOME STUFF WITH THE PARAMETERS
}
Now the issue is the ajax is not calling MyMethod. Any points...
Thanks in advance...
Upvotes: 0
Views: 1351
Reputation: 443
issue resolved...
the array of strings from jQuery can be read as a whole string at the code behind. example:
var myArray[] = {"string 1", "string 2", "string 3"};
will be
string myValue = "string 1,string 2,string 3";
in the serverside code
Upvotes: 1
Reputation: 7297
Try this:
var myParam = {'strArray' : myArray, 'id' : x };
$.ajax({
type: "POST",
url: "myurl/MyMethod",
data: $.param(myParam),
dataType: "json",
success: function(response){
// DO SOME STUFF WITH THE RESPONSE...
}
});
If you are using MVC, and your input elements match the values you are passing back, you can just serialize all the inputs: data: $('form').serialize(),
Upvotes: 0