Is it possible to alter http request's header using javascript?

前端 未结 2 785
夕颜
夕颜 2021-01-12 04:52

Is it possible to use some kind of JavaScript to change or set a HTTP request\'s header?

2条回答
  •  情歌与酒
    2021-01-12 05:29

    Using the XMLHttpRequest object, you can use the setRequestHeader function.

    A little code to get you on your way:

    var xhr = new XMLHttpRequest()
    xhr.open("GET", "/test.html", true);
    xhr.setRequestHeader("Content-type", "text/html");
    
    xhr.send();
    

    The method setRequestHeader must be called after open, and before send.

    More info: https://developer.mozilla.org/en/DOM/XMLHttpRequest#setRequestHeader()

提交回复
热议问题