set referer url with ajax request

我们两清 提交于 2019-11-29 17:32:30

问题


I want to set the referer page while sending an ajax request. I have done this way but it didn't work.

I have included this javascript in a local html file and the main url is cross domain.

$.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

What is wrong in this script or there is another way of doing this?


回答1:


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);
  }
});


来源:https://stackoverflow.com/questions/8798706/set-referer-url-with-ajax-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!