我使用以下方法成功发送了一个 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 specification forbids更改 referer
header (这会阻止位于其中的网站绕过某些网站使用 referer 进行的安全检查)。
Terminate these steps if header is a case-insensitive match for one of the following headers:
- …
- Referer
- …
关于javascript - 为 XMLHttpRequest 设置 referer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27218525/