I have 3 AJAX functions to move data between a Django app on my website and some JavaScript using YUI in the browser. There is not a major difference between them in terms of their structure, concept, code, etc. 2 of them work fine, but in the 3rd one, I get one spurious HTTP request immediately after the intended request. Its POST data contains a subset of the POST data of the intended request. The request meta data is identical except for the CONTENT_LENGTH (obviously) and the CONTENT_TYPE which is 'text/plain; charset=UTF-8' for the intended and 'application/x-www-form-urlencoded' for the unwanted request. I do not set the content type explicitely at all which seems to suggest both requests do not have the same origin and the second one just pops out of thin air.
我有3个AJAX函数来在我的网站上的Django应用程序和使用浏览器中的YUI的一些JavaScript之间移动数据。它们的结构,概念,代码等在它们之间没有重大区别。其中2个工作正常,但在第3个中,我在预期的请求之后立即得到一个虚假的HTTP请求。其POST数据包含预期请求的POST数据的子集。除了CONTENT_LENGTH(显然)和CONTENT_TYPE('text / plain)之外,请求元数据是相同的。 charset = UTF-8'用于预期和'application / x-www-form-urlencoded'用于不需要的请求。我根本没有明确地设置内容类型,这似乎暗示两个请求都没有相同的来源,而第二个请求只是凭空而来。
The intended request sets HTTP_CACHE_CONTROL': 'no-cache' and 'HTTP_PRAGMA': 'no-cache', the spurious one does not. The dev server log output for both requests is
预期的请求设置HTTP_CACHE_CONTROL':'no-cache'和'HTTP_PRAGMA':'no-cache',虚假的请求不会。两个请求的开发服务器日志输出是
[15/Feb/2010 15:00:12] "POST /settings/ HTTP/1.1" 200 0
What does the last 0 at the end mean ? Could not find any documentation on that. This value is usually non-zero... In Apache, it is the total size in bytes of the server response, can someone confirm it's the same for Django ?
最后的0结尾是什么意思?找不到任何相关文档。这个值通常是非零的...在Apache中,它是服务器响应的总大小(以字节为单位),有人可以确认它与Django相同吗?
My problem obviously is to find out where this additional request comes from.
我的问题显然是找出这个额外请求的来源。
I am fairly familiar with JS debugging using Firebug and I think I'm good at Python and Django, but I do not know a lot about the internals of HTTP requests and responses. I can breakpoint and step through the JS code that sends the intended XMLHTTP request, but that breakpoint does not get hit again.
我对使用Firebug进行JS调试非常熟悉,我认为我擅长Python和Django,但我对HTTP请求和响应的内部结构并不了解。我可以断点并逐步执行发送预期XMLHTTP请求的JS代码,但该断点不会再被命中。
The problem occurs with both FF3 and Safari, I'm on Snow Leopard, so I can't test with IE or Chrome. I've looked at Django debugging techniques and tools like http://robhudson.github.com/django-debug-toolbar/ but I think I already have the information they can give me.
FF3和Safari都出现问题,我在Snow Leopard上,所以我无法使用IE或Chrome进行测试。我已经看过Django调试技术和工具,比如http://robhudson.github.com/django-debug-toolbar/,但我想我已经掌握了他们可以提供的信息。
Can someone advise on a strategy or a tool to narrow the problem down ?
有人可以就策略或工具提出建议以缩小问题范围吗?
1 个解决方案
#1
1
The problematic AJAX function submits form data, the working two don't. Forms have a default action which takes place when the form is submitted: post a request with the form data. I failed to prevent this default action.
有问题的AJAX函数提交表单数据,而工作两者则没有。表单具有默认操作,该操作在提交表单时发生:使用表单数据发布请求。我无法阻止此默认操作。
So the spurious request did indeed come out of the dark underwood of the browser, there is no code in my js files that sends it.
所以虚假的请求确实来自浏览器的黑暗丛林,我的js文件中没有代码发送它。
Solution:
解:
YAHOO.util.Event.preventDefault(event);
at the beginning of the form submit event handler.
在表单提交事件处理程序的开头。
See also http://developer.yahoo.com/yui/examples/event/eventsimple.html
另请参阅http://developer.yahoo.com/yui/examples/event/eventsimple.html
#1
1
The problematic AJAX function submits form data, the working two don't. Forms have a default action which takes place when the form is submitted: post a request with the form data. I failed to prevent this default action.
有问题的AJAX函数提交表单数据,而工作两者则没有。表单具有默认操作,该操作在提交表单时发生:使用表单数据发布请求。我无法阻止此默认操作。
So the spurious request did indeed come out of the dark underwood of the browser, there is no code in my js files that sends it.
所以虚假的请求确实来自浏览器的黑暗丛林,我的js文件中没有代码发送它。
Solution:
解:
YAHOO.util.Event.preventDefault(event);
at the beginning of the form submit event handler.
在表单提交事件处理程序的开头。
See also http://developer.yahoo.com/yui/examples/event/eventsimple.html
另请参阅http://developer.yahoo.com/yui/examples/event/eventsimple.html