jquery ajax发布查询字符串

时间:2012-03-12 23:12:40

标签: jquery ajax query-string

刚刚掌握这个jquery和ajax的事情。

我想在页面上运行一个小脚本而且我已经收集了我需要使用来自jquery函数的POST来执行此操作。我在发送查询字符时遇到了麻烦,但是我做错了什么?

$.post("inventory.php?use=" + drag_item.attr('id'));

drag_item.attr('id')是一个小字的文字,这是正确的做法吗?

2 个答案:

答案 0 :(得分:1)

您应该对参数进行编码:

$.post("inventory.php", { use: drag_item.attr('id') });

此外,在此示例中,您只发送一个AJAX请求,但从不订阅任何成功回调,以便处理服务器返回的结果。你可以这样做:

$.post("inventory.php", { use: drag_item.attr('id') }, function(result) {
    // this will be executed when the AJAX call succeeds and the result
    // variable will contain the response from the inventory.php script execution
});

还要确保您在此示例中使用的drag_item已正确初始化为某个现有DOM元素,并且此DOM元素具有id属性。

最后在FireFox中使用javascript调试工具(如FireBox或Chrome浏览器中的Chrome开发人员工具栏)调试您的AJAX请求,并查看发送到服务器和从服务器发送的请求和响应以及可能发生的任何错误。< / p>

答案 1 :(得分:1)

$.post("inventory.php?use=" + drag_item.attr('id')); //wrong

这是错误的,为此目的需要额外的一组参数:

$.post("inventory.php",{use:''+drag_item.attr('id')},function(responseData){
    //callback goes here
});
相关问题