gpt4 book ai didi

javascript - setRequestHeader 未添加授权 header

转载 作者:行者123 更新时间:2023-11-28 05:15:27 28 4
gpt4 key购买 nike

我正在尝试从 javascript 发送带有授权 header 的 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 :Unauthorized";从 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 对象表示法:application/json

最佳答案

这是一个跨源浏览器请求吗?如果是的话,这将作为安全措施被阻止,您从哪里发起请求,我可以获取网址,并且任何其他数据可能会有所帮助,很抱歉必须将此作为答案发布无法评论希望这有帮助

P.S-如果您有暂存网址,请在上传代码后尝试从那里尝试可能会起作用

关于javascript - setRequestHeader 未添加授权 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41010038/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com