Adonis Alex
Adonis Alex

Reputation: 23

How to pass array as an argument to server method via jQuery+Ajax

Well I am developing a web application in Asp.Net and in the code behind I am using C#. I've successfully written code to pass a class object to code behind method but I am wondering how to pass an array as an argument via ajax and jQuery.

I've tried something but it didn't work. Here is my code what I am trying to run

function Test(){
var argu = [1, 2];
$.ajax({
        type: 'POST',
        url: 'MyPage.aspx/Foo',
        data: '{args: ' + argu + '}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
           // Success
        },
        error: function (response) {
           // Failed
        }
      });
}

And this is my code behind method which is written in C#.

[WebMethod]
[ScriptMethod]
public static bool Foo(int[] args)
{
   return true;
}

Application built successfully. I have put break points but my code behind method doesn't fire. I am wondering Where is the problem? What else I am missing here?

Upvotes: 1

Views: 510

Answers (3)

David Chelliah
David Chelliah

Reputation: 1349

Why no simply set the array value to data and do the post ?

function Test(){
var argu = [1, 2];
$.ajax({
        type: 'POST',
        url: 'MyPage.aspx/Foo',
        data: argu,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
           // Success
        },
        error: function (response) {
           // Failed
        }
      });
}

You can use JSON.stringify([1,2]) to send the entire data as a string to the backend, get the data as a string (rather than int[]) and deserialize of parse it back to int[]

Upvotes: 1

Asad Ali
Asad Ali

Reputation: 361

Well all code seems good enough to make a call but I think the problem is in passing the data in your code.

It should be like.

data: '{args: ' + JSON.Stringify(argu) + '}'

And finally your code should be like

function Test(){
var argu = [1, 2];
$.ajax({
        type: 'POST',
        url: 'MyPage.aspx/Foo',
        data: '{args: ' + JSON.Stringify(argu) + '}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
           // Success
        },
        error: function (response) {
           // Failed
        }
      });
}

Hope that helps.

Upvotes: 1

charlietfl
charlietfl

Reputation: 171669

Where is the problem?

What you are sending is not valid JSON and that's what you are telling $ajax to send when you set contentType: "application/json; charset=utf-8"

JSON requires double quotes for keys and for string values

Don't try creating JSON manually ... use language built in serializers

Try changing:

data: '{args: ' + argu + '}',

To

data: JSON.stringify({args:  argu }),

Note: Seems you are expecting int in server code. Looks suspicious when you are sending an object structure

Upvotes: 1

Related Questions