setRequestHeader没有添加授权标头

时间:2016-12-07 05:27:41

标签: javascript http-headers

我正在尝试使用javascript的Authorization标头发送一个http POST,并发现许多答案推荐此配置

var device_address = 10.25.148.164;
var device_login = myusername;
var device_password = mypassword;
var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://' + device_address + '/rest/conf', true);
xhr.setRequestHeader('Authorization', 'Basic ' + btoa(device_login + ':' + device_password));
xhr.send();

以上配置对我不起作用,我得到状态码401:未经授权" ;从Wireshark数据包捕获,我看到以下

Hypertext Transfer Protocol
OPTIONS /rest/conf HTTP/1.1\r\n
    [Expert Info (Chat/Sequence): OPTIONS /rest/conf HTTP/1.1\r\n]
        [OPTIONS /rest/conf HTTP/1.1\r\n]
        [Severity level: Chat]
        [Group: Sequence]
    Request Method: OPTIONS
    Request URI: /rest/conf
    Request Version: HTTP/1.1
Host: 10.25.148.164\r\n
Connection: keep-alive\r\n
Access-Control-Request-Method: POST\r\n
Origin: http://10.120.22.225:3000\r\n
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36\r\n
Access-Control-Request-Headers: authorization\r\n
Accept: */*\r\n
Referer: http://10.120.22.225:3000/myapp/1/edit?\r\n
Accept-Encoding: gzip, deflate, sdch\r\n
Accept-Language: en-US,en;q=0.8\r\n
\r\n
[Full request URI: http://10.25.148.164/rest/conf]
[HTTP request 1/1]
[Response in frame: 14]

但是,如果我从Postman发送POST它可以工作,Wireshark数据包捕获显示以下

Hypertext Transfer Protocol
POST /rest/conf HTTP/1.1\r\n
Host: 10.25.148.164\r\n
Connection: keep-alive\r\n
Content-Length: 0\r\n
Accept: application/json\r\n
Cache-Control: no-cache\r\n
Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop\r\n
Authorization: Basic <original_string_removed_from_here>\r\n
    Credentials: myusername:mypassword
Content-Type: application/json\r\n
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36\r\n
Postman-Token: 4ec1726d-dced-eede-625b-17f9b8f3e0fe\r\n
Accept-Encoding: gzip, deflate\r\n
Accept-Language: en-US,en;q=0.8\r\n
\r\n
[Full request URI: http://10.25.148.164/rest/conf]
[HTTP request 1/1]
[Response in frame: 19]

我尝试了window.btoa而不是btoa,我尝试添加

xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Accept', 'application/json');

但没有任何效果。

任何人都可以告诉我我错过了什么?

更新1

我使用了一个登台服务器并启用了Access-Control-Allow-Origin:*,但响应仍然是401 Unauthorized

Hypertext Transfer Protocol
HTTP/1.1 401 Unauthorized\r\n
    [Expert Info (Chat/Sequence): HTTP/1.1 401 Unauthorized\r\n]
        [HTTP/1.1 401 Unauthorized\r\n]
        [Severity level: Chat]
        [Group: Sequence]
    Request Version: HTTP/1.1
    Status Code: 401
    Response Phrase: Unauthorized
Access-Control-Allow-Origin: *\r\n
Content-Type: application/json\r\n
Vyatta-Specification-Version: 0.3\r\n
Cache-Control: no-cache\r\n
Content-Length: 47\r\n
    [Content length: 47]
Date: Sat, 10 Dec 2016 02:25:48 GMT\r\n
Server: lighttpd/1.4.35\r\n
\r\n
[HTTP response 1/1]
[Time since request: 0.000526000 seconds]
[Request in frame: 19]
File Data: 47 bytes

JavaScript Object Notation:application / json

1 个答案:

答案 0 :(得分:0)

这是一个跨域浏览器请求,如果是这样,这将作为安全措施被阻止,你从哪里发起请求我可以得到网址和任何其他数据可能有帮助,抱歉不得不发布这个作为答案无法评论希望这是有帮助的

P.S-如果您有一个临时网址,请在上传代码后从那里尝试