2012-03-12 49 views
1

刚刚掌握了这个jQuery和ajax的东西。jquery ajax post querystring

我想在页面上运行一个小脚本,并且我已经收集到了我需要使用jquery函数中的POST来执行此操作。虽然发送querystrings虽然我有困难,但我做错了什么?

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

drag_item.attr('id')是一个小小的单词文本,是这样做的正确方法吗?

回答

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

这是错误的,它需要用于此目的的另外一组则params的:

$.post("inventory.php",{use:''+drag_item.attr('id')},function(responseData){ 
    //callback goes here 
}); 
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或Google Chrome的Chrome开发人员工具栏中使用JavaScript调试工具(例如FireBug)调试您的AJAX请求,并查看发送到服务器和从服务器发送的请求和响应以及可能发生的任何可能的错误。

+0

感谢,所以我可以使用inventory.php正常$ _GET [用途]方法与工作数据是啊?你所有的DOM说话都让我失去了,但我想我会解决它的。 – user1022585 2012-03-12 23:19:28

+0

@ user1022585,否则你不能在你的服务器端脚本中使用'$ _GET [“use”]'因为你没有发送GET请求。您正在从客户端发送POST请求,因此您应该在服务器上使用'$ _POST [“use”]'来获取相应的值。或修改您的客户端脚本以使用'$ .get'而不是'$ .post'。 – 2012-03-12 23:20:59