我如何使用自定义 header 发送获取请求

How I can send get request with custom header

我需要发送自定义 GET 请求,我使用下一个 javascript

<script type="text/javascript">
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
    var DONE = this.DONE || 4;
    if (this.readyState === DONE){
        alert(this.readyState);
    }
};
request.open('GET', 'my_ip_addr:8080', true);
request.setRequestHeader('exec "myFile.exe"', 'asd'); 

request.send(null);  
</script>

相等的 HTTP 请求

    GET / HTTP/1.1
    exec "myFile.exe"
    Content-Length: 2

但是不要发送我的javascript代码,请帮忙,我写错了什么? 谢谢

查看您的 JavaScript 错误控制台:

Uncaught SyntaxError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'exec "myFile.exe"' is not a valid HTTP header field name.

您可能想要更多类似的东西:

// Use X- prefixes for non-standard headers
request.setRequestHeader('X-exec', "myFile.exe");