X-Requested-With/HTTP_X_REQUESTED_WITH 奇怪的问题

发布于 2024-07-12 06:47:35 字数 383 浏览 5 评论 0原文

我正在构建一个 Django 网站并尝试使用 request.is_ajax() 函数...但是它只能在本地工作并且正在驱动我疯了!

我正处于刚刚转储标题的位置。 这里(在 django 测试服务器上)有 HTTP_X_REQUESTED_WITH 但在生产服务器(cherokee+scgi)上我得到的只是 X-Requested-With

我使用 firebug 来监听发送标头,它是X-Requested-With(在网站的两个版本上)。 我非常非常困惑。 谁能解释一下发生了什么以及我如何在不失去理智的情况下解决它?

I'm building a Django site and trying to use the request.is_ajax() function... But it's only working locally and it's driving me crazy!

I'm at the point where I've just dumped the headers. Here (on the django test server) there's HTTP_X_REQUESTED_WITH but on the production server (cherokee+scgi) all I get is X-Requested-With.

I've used firebug to snoop the sent headers and it's X-Requested-With (on both versions of the site). I'm very, very confused. Can anybody explain what's happening and how I can work around it without losing my mind?

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

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

发布评论

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

评论(2

和影子一齐双人舞 2024-07-19 06:47:35

wrt/ X-Requested-With => HTTP_X_REQUESTED_WITH东西,它符合CGI规范。 由于 FastCGI、SCGI 和 WSGI 都基于 CGI 规范,Django 开发人员选择遵守此约定(FWIW,ModPythonRequest 类进行相同的重写以保持一致性)。

所以看来你的问题是 cherokee/scgi 链中的某些内容没有正确重写标头。 您使用哪种 scgi 实现?

wrt/ the X-Requested-With => HTTP_X_REQUESTED_WITH stuff, it's in conformance with the CGI specifications. Since FastCGI, SCGI and WSGI are all based on the CGI specs, Django developpers choosed to stick to this convention (FWIW, the ModPythonRequest class do the same rewrite for consistency).

So it seems that your problem is that something in the cherokee/scgi chain doesn't rewrite the headers correctly. Which scgi implemetation are you using ?

A君 2024-07-19 06:47:35

我目前正在使用一点中间件来解决这个问题,它本质上是寻找“错误”的标头,如果存在,则附加一个具有相同值的新标头:

if 'X-Requested-With' in request.META:
    request.META['HTTP_X_REQUESTED_WITH'] = request.META['X-Requested-With']

但我真的希望我知道这些应该发生什么标头,因为总是发送 X-Requested-With...我不明白为什么应该将其转换为 HTTP_X_REQUESTED_WITH 以及为什么不这样做。

编辑:原因似乎是在实际的 Web 服务器内部。

case 'X':
    if (header_equals ("X-Forwarded-For", header_x_forwarded_for, begin, header_len)) {
        ret = add_known_header (hdr, header_x_forwarded_for, val_offs, val_len);
    } else if (header_equals ("X-Forwarded-Host", header_x_forwarded_host, begin, header_len)) {
        ret = add_known_header (hdr, header_x_forwarded_host, val_offs, val_len);
    } else
        goto unknown;
    break;

我已经提交了一个错误来添加我的标头,但是所有 X-* 标头都应该转换为 HTTP_X_* 标头吗?

I'm currently working around the issue with a tiny bit of MiddleWare that essentially looks for the "wrong" header and if it exists, appends a new header of the same value:

if 'X-Requested-With' in request.META:
    request.META['HTTP_X_REQUESTED_WITH'] = request.META['X-Requested-With']

But I really wish I knew what was supposed to happen with these headers because X-Requested-With is always sent... I don't see why that should be translated in to HTTP_X_REQUESTED_WITH and why it's not.

Edit: The cause appears to be deep within the actual web server.

case 'X':
    if (header_equals ("X-Forwarded-For", header_x_forwarded_for, begin, header_len)) {
        ret = add_known_header (hdr, header_x_forwarded_for, val_offs, val_len);
    } else if (header_equals ("X-Forwarded-Host", header_x_forwarded_host, begin, header_len)) {
        ret = add_known_header (hdr, header_x_forwarded_host, val_offs, val_len);
    } else
        goto unknown;
    break;

I've filed a bug to get my header added but should all X-* headers be converted into HTTP_X_* headers?

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