我在Django後端使用jQuery(1.4.2)並使用Firefox(3.5.15)在Debian上進行我的開發。我使用$ .ajax()將一些數據發佈到我的服務器,這反映了數據返回到瀏覽器。 Firefox正在發送POST,然後立即在同一個URL上執行GET。這個GET從哪裏來?我不希望它發生。jQuery ajax POST導致立即獲取到相同的URL
我使用POST代碼看起來是這樣的:
$.ajax({
type: 'POST',
url: '/edit_value/',
data: JSON.stringify(data_to_post),
success: function(updated) {
alert('success');
},
error: function(request, description, exception) {
alert('error');
},
async: false,
dataType: 'json'
});
我看到了成功的警告消息,而不是錯誤警報消息。看着我的服務器日誌,我正確地回顯了data_to_post。
我看過tcpdump並查看POST請求。它看起來完全一樣我希望:
POST /edit_value/ HTTP/1.1
Host: localhost:8000
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.15) Gecko/20101028 Iceweasel/3.5.15 (like Firefox/3.5.15)
Accept: application/json, text/javascript, */*
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://localhost:8000/
Content-Length: 30
Cookie: csrftoken=9e3edd79a51956f088f4a505ca1b4282; sessionid=80d72430f4682632ccfb8dc8047b7d17
Pragma: no-cache
Cache-Control: no-cache
[{"id":"161","value":"988.0"}]
響應看起來正常過:
HTTP/1.0 200 OK
Date: Mon, 22 Nov 2010 07:28:44 GMT
Server: WSGIServer/0.1 Python/2.6.6
Vary: Cookie
Content-Type: text/json
[{"id":"161","value":"988.0"}]
如果異步設置爲true,有時瀏覽器會掛斷TCP連接會提早,有時請求/響應完成。通過將async設置爲false,請求/響應始終完成(對於上例,它被設置爲false)。然後Firefox立即發送一個GET。
GET /edit_value/ HTTP/1.1
Host: localhost:8000
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.15) Gecko/20101028 Iceweasel/3.5.15 (like Firefox/3.5.15)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://localhost:8000/
Cookie: csrftoken=9e3edd79a51956f088f4a505ca1b4282; sessionid=80d72430f4682632ccfb8dc8047b7d17
GET從哪裏來?如何在頁面重新加載之前將其捕獲到Firebug中?我可以提供哪些其他信息?
謝謝, 克雷格
什麼時候(事件)/如何調用$ .ajax()? – 2010-11-22 07:47:40