Adding a query string to an Ajax url call

I want to be able to pass the query string along with the url when we call jQuery Ajax;

Example:

    $.ajax({
           type: "POST",
           url: "index.php?task=addNewInfo",
           data: $('#regForm').serialize(),
           dataType: "json",
              .....
      });  

So the query string from param task is working fine? or do we need to do it differently? Thank.

+3
source share
3 answers

Send the task to the data parameter

data:"task=addNewInfo&" + $('#regForm').serialize()

This is used to use the POST method. If you want to use the GET method, the Arun solution will work fine.

+8
source

I think the following will work just fine

url : "index.php?task=addNewInfo&" + $('#regForm').serialize()

? - . , POST.

+2

Yes. The request string and request body are two different things in HTTP requests. jQuery wraps data in a query string for GET requests, which is probably a source of confusion

  • GET request: the body (or payload) is empty, only the URL contains data.
  • POST request: fill in the body as you like, with url encoding, like you, or with json-encoding
0
source

All Articles