jQuery send GET and POST parameters simultaneously at AJAX request

亡梦爱人 提交于 2020-05-12 16:39:12

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/12680522/jquery-send-get-and-post-parameters-simultaneously-at-ajax-request

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!