admin管理员组

文章数量:1389758

I have some code that work, but I need to add some variable to the data sent by ajax.
I know, I can put this variable in the array sended, but it's no good for me.
I want to add +'&count='+'second'

Maybe I can send second array, but how?

var arr = new Array(1,2,3,4);
$.ajax({
    type: 'POST',
    url: baseUrl + "search/simple_filter",
    data: {arr: arr}+'&count='+'second',
    success: function (data) {
        if (!data) console.log(data);
    }
});

I have some code that work, but I need to add some variable to the data sent by ajax.
I know, I can put this variable in the array sended, but it's no good for me.
I want to add +'&count='+'second'

Maybe I can send second array, but how?

var arr = new Array(1,2,3,4);
$.ajax({
    type: 'POST',
    url: baseUrl + "search/simple_filter",
    data: {arr: arr}+'&count='+'second',
    success: function (data) {
        if (!data) console.log(data);
    }
});
Share Improve this question edited Jul 31, 2014 at 19:21 Simon Arnold 16.2k8 gold badges68 silver badges88 bronze badges asked Jul 31, 2014 at 19:16 danildanil 1171 gold badge2 silver badges6 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 4

Expand your base object, like so:

  data: {arr: arr, count : 'second'}
var arr = new Array(1,2,3,4);
             $.ajax({
             type: 'POST',
             url: baseUrl + "search/simple_filter",
             data: {'arr': arr, 'count': second},
             success: function (data) {
             if (!data) 
             console.log(data);
             }
             });

To add a query string parameter in a POST request you would add it to the URL:

url: baseUrl + "search/simple_filter?count=" + second,
data: { arr: arr },

I assume that you want to use the value of the variable second, not the string "second".

If you want to add the parameter in the POST data, and not as a query string parameter, you would add it to the object for the data property:

url: baseUrl + "search/simple_filter",
data: { arr: arr, count: second },

You should add that to the POST URL and not the body:

 var arr = new Array(1,2,3,4);
     $.ajax({
          type: 'POST',
          url: baseUrl + "search/simple_filter"+'&count='+'second',
          data: {arr: arr},
          success: function (data) {
             if (!data) 
              console.log(data);
         }
      });

本文标签: javascriptSend two arrays or array with variable via ajaxStack Overflow