Django:如何追踪虚假的 HTTP 请求?

发布于 2024-08-21 07:17:56 字数 1070 浏览 3 评论 0原文

我有 3 个 AJAX 函数来在我的网站上的 Django 应用程序和浏览器中使用 YUI 的一些 JavaScript 之间移动数据。它们在结构、概念、代码等方面没有重大区别。其中 2 个工作正常,但在第 3 个中,我在预期请求之后立即收到一个虚假 HTTP 请求。其 POST 数据包含预期请求的 POST 数据的子集。除了 CONTENT_LENGTH(显然)和 CONTENT_TYPE 为“text/plain;”之外,请求元数据是相同的。 charset=UTF-8' 用于预期请求,'application/x-www-form-urlencoded' 用于不需要的请求。我根本没有明确设置内容类型,这似乎表明两个请求没有相同的来源,而第二个请求只是凭空弹出。

预期请求设置了 HTTP_CACHE_CONTROL': 'no-cache' 和 'HTTP_PRAGMA': 'no-cache',而虚假请求则没有。两个请求的开发服务器日志输出都是

[15/Feb/2010 15:00:12] "POST /settings/ HTTP/1.1" 200 0

最后一个 0 是什么意思?找不到任何相关文档。该值通常非零...在 Apache 中,它是服务器响应的总大小(以字节为单位),有人可以确认它对于 Django 来说是相同的吗?

我的问题显然是找出这个额外的请求来自哪里。

我对使用 Firebug 进行 JS 调试相当熟悉,并且我认为我擅长 Python 和 Django,但我对 HTTP 请求和响应的内部了解不多。我可以断点并单步执行发送预期 XMLHTTP 请求的 JS 代码,但该断点不会再次被命中。

FF3 和 Safari 都会出现此问题,我使用的是 Snow Leopard,所以无法使用 IE 或 Chrome 进行测试。 我研究了 Django 调试技术和工具,例如 http://robhudson.github.com/ django-debug-toolbar/ 但我想我已经有了他们可以给我的信息。

有人可以建议一种策略或工具来缩小问题范围吗?

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.

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

[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 ?

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.

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.

Can someone advise on a strategy or a tool to narrow the problem down ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

女皇必胜 2024-08-28 07:17:56

有问题的 AJAX 函数提交表单数据,而工作的两个函数则不提交。表单有一个默认操作,该操作在提交表单时发生:使用表单数据发布请求。我未能阻止此默认操作。

因此,虚假请求确实来自浏览器的黑暗丛林,我的 js 文件中没有发送该请求的代码。

解决方案:

YAHOO.util.Event.preventDefault(event);

在表单的开头提交事件处理程序。

另请参阅 http://developer.yahoo.com/yui/examples/event/ eventssimple.html

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.

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文