Jquery在ajax请求时同时发送GET和POST参数

时间:2012-10-01 20:24:32

标签: jquery ajax post get

如何同时使用jquery ajax请求发送GET和POST参数?

我正在尝试将do=ajax&id=" + ID添加到url,但结果请求仅在没有查询字符串的情况下为sss.php打磨(获取部分)。感谢。

$.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 个答案:

答案 0 :(得分:9)

我认为你得到了一个观察错误,或者看到了服务器端而不是jQuery问题。当我发帖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");
  }
});

...查询字符串和POST数据都发送到服务器。如果您使用Chrome或Firefox等现代浏览器,并在触发帖子后查看控制台的“网络”标签,则可以轻松查看此内容。就我而言:

Image showing post with both query string and form data

(您可以忽略上面的服务器回复403; JSBin不允许POST,但这不会影响我们在去往服务器的请求中看到的内容。)

所以这里的答案是:仔细检查你是如何获得数据服务器端的。 URL中的参数(“GET”样式参数)可用作查询字符串参数(URL的一部分); “POST”样式参数可用作“表单”数据(例如,响应的主体)。根据您使用的服务器端技术,通常有不同的方法来检索GET(查询字符串)参数与POST(表单数据/正文)参数。