admin管理员组文章数量:1316673
I am currently trying to set up a POST request to an REST API (Cloudsight) with basic authorization. My code so far is:
var xhr = new XMLHttpRequest();
xhr.open("POST", "", true);
xhr.setRequestHeader("Authorization:", "CloudSight [key]");
xhr.setRequestHeader("Content-Type", ".jpg");
xhr.send(null);
console.log(xhr.status);
console.log(xhr.statusText);
I am currently trying to set up a POST request to an REST API (Cloudsight) with basic authorization. My code so far is:
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://api.cloudsightapi./image_requests", true);
xhr.setRequestHeader("Authorization:", "CloudSight [key]");
xhr.setRequestHeader("Content-Type", "http://previews.123rf./images/valzann/valzann1412/valzann141200061/34262193-cigarette-end-on-a-white-background-Stock-Photo.jpg");
xhr.send(null);
console.log(xhr.status);
console.log(xhr.statusText);
When I try and run it I get the error:
Uncaught SyntaxError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'Authorization:' is not a valid HTTP header field name.
Does anyone know what is wrong with the code? I am very new to JavaScript and APIs but would very much appreciate an answer. (Here's the link to Cloudsight's documentation if it helps: https://cloudsight.readme.io I have looked through it and tried to find other Authorization HTTP Header JavaScript examples without success.)
Share Improve this question edited Dec 11, 2023 at 19:44 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Oct 12, 2015 at 14:58 teamshortcutteamshortcut 3001 gold badge5 silver badges20 bronze badges 1-
You have a
:
at the end ofAuthorization:
, remove that and test it again. – t.niese Commented Oct 12, 2015 at 15:01
1 Answer
Reset to default 7You don't need the :
when supplying headers. Remove them from:
xhr.setRequestHeader("Authorization:", ...
The request now passes but returns some other error, which you have to handle on your part. I guess it wont be a problem.
This goes for every header, not only for Authorization
. Hope this resolves your problem.
本文标签: Javascript Authorization HTTP HeadersStack Overflow
版权声明:本文标题:Javascript Authorization HTTP Headers - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742001644a2411132.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论