HTTP_X_REQUESTED_WITH不能正常工作在Firefox 4 [英] HTTP_X_REQUESTED_WITH not working properly in firefox 4

查看:155
本文介绍了HTTP_X_REQUESTED_WITH不能正常工作在Firefox 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个code重定向我的用户,阻断从他们的浏览器的AJAX只页

I'm using this code to redirect my users, blocking ajax-only pages from their browser

if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && 
    strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {}
else {
    header("Location: /");
}

它工作正常,在谷歌Chrome,火狐26和IE11,不过在Firefox 4,用ajax加载时头部被触发,即使。

It works fine on Google chrome, Firefox 26 and IE11, however in firefox 4, the header is triggered even when loading using ajax.

我该如何解决这个问题?

How can I fix this?

推荐答案

您可以尝试两种设置HTTP_X_REQUESTED_WITH头自己,或者设置不同的页眉和检查也:

You could try to either set the HTTP_X_REQUESTED_WITH header yourself, or set a different header and check for it also:

$.ajaxSetup({
        beforeSend: function (request)
        {
            request.setRequestHeader("HTTP_X_REQUESTED_WITH",'xmlhttprequest');
            request.setRequestHeader("BACKUP_FIREFOX_AJAX", 'xmlhttprequest');
        }
 });

然后

if((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && 
   strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') || 
   (isset($_SERVER['BACKUP_FIREFOX_AJAX']) && 
   strtolower($_SERVER['BACKUP_FIREFOX_AJAX']) == 'xmlhttprequest'))

不知道这是否会赋予它的火狐4(真的老版本),但它是值得一试。

Not sure if it will work given it's firefox 4 (really old version), but it's worth a shot.

好了,就挖一些,似乎有一个古老的Firefox漏洞,其中1)如果XHR被重定向然后将自定义标题都将丢失,和2)当自动代理检测正在运行的Firefox有时会做一个内部的重定向从而引发问题1。

Ok, on digging some more, there appears to be an old Firefox bug where 1) if an xhr gets redirected then the custom headers are lost, and 2) When "automatic proxy detection" is operating firefox will sometimes do an internal redirect which triggers the problem in 1.

所以,你可能需要做一些事情以外的头......也许追加查询字符串参数向所有的出Ajax请求,我不知道,如果你需要直接修改URL或GET请求数据,所以我只是两者都做,并希望它的工作原理:

So, you may need to do something other than a header... perhaps append a query string param to all outgoing ajax requests, I'm not sure if you'd need to modify the url directly or data for GET requests, so I'd just do both and hope it works:

$.ajaxSetup({
  beforeSend: function(jqXHR, settings) {
      if (settings.url.split('?').length > 1) {
         settings.url = settings.url + '&ajax=1';
      }
      else {
          settings.url = settings.url + '?ajax=1';
      }
   },
data: {
    ajax: '1'
  }
});

,然后你可以这样做:

and then you could do:

if((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && 
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') || 
$_GET['ajax']==1)

这篇关于HTTP_X_REQUESTED_WITH不能正常工作在Firefox 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆