jQuery的AJAX POST导致立即到达相同的URLAJAX、jQuery、URL、POST

2023-09-10 19:39:47 作者:浮光月影

我使用jQuery(1.4.2)与Django的后端,做我的开发与Firefox(3.5.15)Debian的。我使用$阿贾克斯()来发布一些数据到我的服务器,它反映了数据返回给浏览器。火狐在发送POST,然后立即执行上同样的URL GET。这哪里是GET哪里来的?我不希望它发生。

I'm using jQuery (1.4.2) with a Django backend and doing my development with Firefox (3.5.15) on Debian. I'm using $.ajax() to post some data to my server, which reflects the data back to the browser. Firefox is sending the POST, then immediately performing a GET on the same URL. Where is this GET coming from? I don't want it to happen.

在code我使用POST看起来是这样的:

The code I'm using to POST looks like this:

$.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正确。

I see the success alert message and not the error alert message. Looking at my server logs, I am echoing back data_to_post correctly.

我看过tcpdump和看到POST请求。它看起来正是我所期望的:

I've watched tcpdump and see the POST request. It looks exactly as I would expect:

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"}]

响应看起来正常过:

The response looks normal too:

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连接,有时请求/响应完成。通过设置异步为false,请求/响应总是在完成(它被设置为false上面的例子)。火狐然后立即发送GET。

If async is set to true, sometimes the browser will hang-up the TCP connection early, sometimes the request/response completes. By setting async to false, the request/response always completes (it was set to false for the above example). Firefox then immediately sends a 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陷阱之前,页面重新加载?我可以提供其他什么信息?

Where does this GET come from? How can I trap it in Firebug before the page reloads? What other information can I provide?

谢谢, 克雷格

推荐答案

我有,当我做一个Ajax请求值范围内类似的问题一个<形式GT; 标签。

I had a similar problem when I was making an Ajax request for values within a <form> tag.

我的问题出现了,因为我有这样的窗体上的操作(例如&LT;形式的行动=mypage.aspx...... )的 - 尽管Ajax的POST被成功 - 是导致表单提交

My problem arose because I had an action on that form (e.g. <form action="mypage.aspx"...) that - even though the Ajax POST was succeeding - was causing the form to be submitted.

如果这是与您的情况,请尝试要么拆除行动宣言表单中的标签或包括返回false; 语句在你的Ajax请求

If this is the same as your situation, try either removing the action declaration in your form tag or including a return false; statement on your Ajax request.