4

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.

6
  • You're using POST. your serialized data is the... "query string"... or more accurately, the parameters you're sending. It only becomes a query string if you're using GET. Commented May 11, 2012 at 3:07
  • It seems you are accessing task through $_GET and other data through $_POST. You should send the task in the data instead of querystring. Commented May 11, 2012 at 3:41
  • Yes. Why not just try it and see. Commented Jan 4, 2014 at 9:36
  • 1
    @GregPettit — The query string component of the URL is the query string no matter what HTTP verb is used for the request. Commented Jan 4, 2014 at 9:36
  • Could you please unaccept my answer? Commented Jan 4, 2014 at 12:42

3 Answers 3

9

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

0

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

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.