【问题标题】:Set referer for XMLHttpRequest?为 XMLHttpRequest 设置引用者?
【发布时间】:2014-11-30 21:36:33
【问题描述】:

我通过以下方式成功发送了XMLHttpRequest

var createCORSRequest = function(method, url) {
  var xhr = new XMLHttpRequest();
  if ("withCredentials" in xhr) {
    // Most browsers.
    xhr.open(method, url, true);
  } else if (typeof XDomainRequest != "undefined") {
    // IE8 & IE9
    xhr = new XDomainRequest();
    xhr.open(method, url);
  } else {
    // CORS not supported.
    xhr = null;
  }
  return xhr;
};

var url = 'http://www.whatismyip.com';
var method = 'GET';
var xhr = createCORSRequest(method, url);

xhr.onload = function() {
  // Success code goes here.
};

xhr.onerror = function() {
  // Error code goes here.
};


xhr.setRequestHeader('referer', 'http://www.google.com');
xhr.send();

但是,我无法定义我的referer。添加自定义referer的正确方法是什么?

【问题讨论】:

  • 我有同样的问题,不明白为什么 XMLHttpRequest 没有推荐人或用户代理信息?有没有办法使用 fetch 或 axios 来捕获它们?

标签: javascript jquery ajax internet-explorer


【解决方案1】:

你不能。 XMLHttpRequest specification forbids 更改了 referer 标头(这可以阻止位于其中的网站绕过某些网站使用引用的安全检查)。

如果标头与以下标头之一不区分大小写,则终止这些步骤:

  • 参考

【讨论】:

    【解决方案2】:

    你可以试试这样的:

    xhr.setRequestHeader('X-Referer', window.location.href);
    

    然后阅读这个自定义的 X-Referer 标头。

    【讨论】:

      【解决方案3】:

      https://www.trustedsec.com/blog/setting-the-referer-header-using-javascript/找到答案

      您可以使用window.history.replaceState(null, '', 'https://yourwebsite.com/forged/referer')进行设置

      据我所知,它只适用于同一个域,但您可以通过这种方式伪造路径。

      https://developer.mozilla.org/en-US/docs/Web/API/History/replaceState

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-06-19
        • 2013-10-13
        • 1970-01-01
        • 1970-01-01
        • 2017-03-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多