Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery send GET and POST parameters simultaneously at AJAX request

How to send GET and POST parameters with jQuery AJAX request simultaneously?

I am trying to add do=ajax&id=" + ID to url, but as the result request sanded only to sss.php without query string (get part). thanks.

$.ajax({
    url: "sss.php?do=ajax&id=" + ID ,
    type: "post",
    data: "ddd=sss",
    // callback handler that will be called on success
    success: function(response, textStatus, jqXHR){
        // log a message to the console
        console.log("Hooray, it worked!");
    },
    // callback handler that will be called on error
    error: function(jqXHR, textStatus, errorThrown){
        // log the error to the console
        console.log(
            "The following error occured: "+
            textStatus, errorThrown
        );
    }
});
like image 917
abrahab Avatar asked Oct 01 '12 20:10

abrahab


1 Answers

I think you're getting an observational error, or seeing a server-side rather than jQuery problem. When I do a post like this:

$.ajax({
  url: "http://jsbin.com/eduzif/1?foo=bar",
  type: "post",
  data: "baz=doh",
  success: function() {
    display("Done, look at your console's network tab");
  }
});

...both the query string and POST data are sent to the server. It's easy to check this if you use a modern browser like Chrome or Firefox and look in the Network tab of the console after triggering the post. In my case:

Image showing post with both query string and form data

(You can ignore that the server above replied with 403; JSBin doesn't allow POST, but that doesn't affect what we see in the request going to the server.)

So the answer here is: Double-check how you're getting the data server-side. The parameters in the URL ("GET" style parameters) are available as query string parameters (part of the URL); the "POST" style parameters are available as "form" data (e.g., the body of the response). Depending on the server-side technology you're using, usually there are different ways to retrieve GET (query string) parameters vs. POST (form data / body) parameters.

like image 125
T.J. Crowder Avatar answered Oct 20 '22 05:10

T.J. Crowder