H.dev.guy
H.dev.guy

Reputation: 189

Adding query string to Ajax url call

I wanna kn is it possible to pass query string along with URL when we call on Jquery Ajax;

Example :

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

So does the query string from the param task works fine? or we need to do it the other way? Thank you.

Upvotes: 4

Views: 36115

Answers (3)

user1125394
user1125394

Reputation:

Yes. query-string and request body are 2 different things in HTTP requests. jQuery wrap the data in the query-string for GET requests, which is probably the source of confusion

  • GET request: the body (or payload) is empty, only the url holds data.
  • POST request: fill the body as you like, either with url-encoding like you did, or with json-encoding

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

I think the following will work fine

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

But why do you want to pass the form values as query params? The post request will pass the values as request params anyway. These params will be sent via the request body which is why you are using POST request type.

Upvotes: 4

Imdad
Imdad

Reputation: 6032

Send the task in the data parameter

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

This is for using POST method. If you want to use GET method then Arun's solution will work fine.

Upvotes: 9

Related Questions