jQuery 使用 ajax 请求设置引用 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8798706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
set referer url with ajax request
提问by Gagan
I want to set the referer page while sending an ajax request. I have done this way but it didn't work.
我想在发送 ajax 请求时设置引用页面。我已经这样做了,但没有用。
I have included this javascript in a local html file and the main url is cross domain.
我已将此 javascript 包含在本地 html 文件中,并且主 url 是跨域的。
$.ajax({
url: "{{main url}}",
dataType: "json",
beforeSend: function(xhr){
xhr.setRequestHeader('X-Alt-Referer', '{{referer url}}');
},
success: function(data){
console.log(data);
}
});
I got some hint from this url
我从这个网址得到了一些提示
Set a request header in JavaScript
I get
我得到
"NetworkError: 404 Not Found - {{main url}}"
error when i tried it from firefox console
我从 Firefox 控制台尝试时出错
What is wrong in this script or there is another way of doing this?
这个脚本有什么问题,或者有其他方法可以做到这一点?
回答by Serg
try to use next code:
尝试使用下一个代码:
var main_url = "http://www.example1.com";
var referrer = "http://www.example2.com";
$.ajax({
url: main_url,
dataType: "json",
headers: {'X-Alt-Referer': referrer },
success: function(data){
console.log(data);
}
});