Search code examples
jqueryajaxpostget

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

Solution

  • 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.