原生JS XMLHttpRequest.setRequestHeader不生效?

使用JS请求某个接口时,不加请求头会403,但通过XMLHttpRequest.setRequestHeader设置请求头好像不生效?
代码里已经设置了,但实际请求的时候没有看到。

http.open('GET',url,false);

http.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36");

http.setRequestHeader("referer", "http://www.oslang.cn/");

http.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");

http.send(http)

image.png

阅读 2.5k
2 个回答
var http = new XMLHttpRequest();
http.open('GET', url, false);
http.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36");
http.setRequestHeader("referer", "http://www.oslang.cn/");
http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
http.send();
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏