0

I have a doubt about the data fields of ajax function.

Usually, we have that a syntax for ajax function may be:

$.ajax({
        url: "/aaa/bbb/ccc",
        method: "SomeMethod",
        data: someData,
        success: function (response) {
        do something
    }

My question is: in data fields, can i put more than one data? In other words, can I pass from:

data: someData,

to:

data: data1, data2, data3...

and so on?

2
  • You can send data as associative array. Commented Jul 25, 2017 at 12:34
  • @luca Sepe you can pass json also,provided you handle that in your server side code. Commented Jul 25, 2017 at 12:36

6 Answers 6

2

You can create an object that holds the data. data: {date1Name: data1Value, date2Name: data2Value}.

Your complete code should look like this.

$.ajax({
        url: "/aaa/bbb/ccc",
        method: "SomeMethod",
        data: {date1Name: data1Value, date2Name: data2Value},
        success: function (response) {
        do something
    }
Sign up to request clarification or add additional context in comments.

Comments

2

You can create an object of key/value pairs.

$.ajax({
    ...
    data : { data1: 'bar', data2: 'foo' },
    ...
});

Comments

1

Here is how you can build multiple params, hold them in an object and send them as JSON.stringify():

var paramsToSend = {};
paramsToSend['data1'] = 'data1';
paramsToSend['data2'] = 'data2';
$.ajax({
    ...
    data: {params:JSON.stringify(paramsToSend)},
    ...
});

Comments

1
$.ajax({
    url: "/aaa/bbb/ccc",
    method: "SomeMethod",
    data: "name=value&name1=value1&name2=value2",
    success: function (response) {
      //do something
    }
 });

Comments

1
$.ajax({
    url: "/aaa/bbb/ccc",
    method: "SomeMethod",
    data: {name : 'Luca', country : 'NA'},
    success: function (response) {}

})

Comments

1

It looks like you want to pass it as an array?

$.ajax({
    url: "/aaa/bbb/ccc",
    method: "SomeMethod",
    data: { data:[data1, data2, data3] },
    success: function (response) {
    do something
}

I would recommend putting the array in a dictionary / JSON object so you have a variable name to key off of in whatever backend language you are using.

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.