2012-10-01 202 views
7

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

我试图将do=ajax&id=" + ID添加到url,但因为结果请求只打磨到sss.php而没有查询字符串(get part)。谢谢。

$.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 
     ); 
    } 
}); 
+0

尝试使用'$ .post'而不是'$ .ajax' – defuz

+1

@defuz:他/她*使用'post',用于所有意图和目的。 ['post'](http://api.jquery.com/jQuery.post/)仅仅是'type:“post”'请求的便捷包装。 –

+0

对我而言,您的解决方案正在发挥作用。它也应该适合你。开始并检查您的Chrome网络控制台,以查看发送给服务器的请求。 –

回答

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(表单数据/主体)参数。

+0

感谢,复查,复制和过去的代码,现在似乎它的工作。不知道最新版本的代码是什么问题:(谢谢 – abrahab

+0

+1,明白了, SO'教给我一些东西。 –