2012-03-12 48 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