2

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);
    }
});
0

4 Answers 4

4

Expand your base object, like so:

  data: {arr: arr, count : 'second'}
Sign up to request clarification or add additional context in comments.

Comments

1
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);
             }
             });

Comments

1

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 },

Comments

1

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);
         }
      });

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.