![](/img/trans.png)
[英]Request header field Access-Control-Allow-Headers is not allowed by Access-Control-Allow-Headers in preflight response
[英]XMLHttpRequest Request header field postman-token is not allowed by Access-Control-Allow-Headers in preflight response
我正在嘗試訪問 smartthings API 以在本地控制設備。 當我使用以下 chrome 擴展時,它確實有效,但是我不想依賴這樣的東西: https : //chrome.google.com/webstore/detail/allow-cors-access-control/lhobafahddgcelffkeicbaginigeejlf?hl=en
當我不使用擴展時,我收到以下錯誤:
Access to XMLHttpRequest at 'https://api.smartthings.com/v1/devices/XXXX-
XXXX-XXXX/commands'from origin 'http://localhost:8080' has been blocked by
CORS policy: Request header field postman-token is not
allowed by Access-Control-Allow-Headers in preflight response.
我一直在試圖了解 CORS 以及為什么我被“拒絕”可以這么說,但我感到困惑的是我的響應/請求標頭似乎是正確的。 以下是請求和響應標頭:
GENERAL
Request URL: https://api.smartthings.com/v1/devices/XXXX-XXXX-XXXX/commands
Request Method: OPTIONS
Status Code: 204 No Content
Remote Address: 1x.xxx.1xx.xx:xxx
Referrer Policy: no-referrer-when-downgrade
RESPONSE HEADERS
Access-Control-Allow-Headers: DNT,Keep-Alive,User-Agent,If-Modified-Since,Cache-Control,Content-Type,Accept,Authorization,X-ST-Client,X-ST-Api-Version,X-ST-Client-AppVersion,X-ST-Client-OS,X-ST-Client-DeviceModel
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 1728000
Connection: keep-alive
Content-Length: 0
Content-Type: text/plain charset=UTF-8
Date: Tue, 06 Aug 2019 16:19:52 GMT
Server: openresty
REQUEST HEADERS
Provisional headers are shown
Access-Control-Request-Headers: authorization,cache-control,content-type,postman-token
Access-Control-Request-Method: POST
Origin: http://localhost:8080
Referer: http://localhost:8080/compare/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/xxx.xx (KHTML, like Gecko) Chrome/xx.x.xxxx.xxx Safari/xxx.xx
我對此的困惑是 smartthings 響應似乎允許 *,這讓我覺得我不應該收到此錯誤。 但是,我對如何從我的最后編輯/調整這些標題感到有些困惑,本質上我想做任何 chrome 擴展正在做的事情? 這是我調用 smartthings API 的方式:
function APIAWAY(){
var settings = {
"async": true,
"crossDomain": true,
"url": "https://api.smartthings.com/v1/devices/XXX-XXXX/commands",
"method": "POST",
"headers": {
"Authorization": "Bearer XXXX-XXXX-XXXX",
"Content-Type": "application/json",
"Accept": "*/*",
"Cache-Control": "no-cache",
"Postman-Token": "XXXX-XXXX-XXXX",
"cache-control": "no-cache"
},
"processData": false,
"data": "{\r\n\"commands\": [\r\n{\r\n\"component\": \"main\",\r\n\"capability\": \"switch\",\r\n\"command\": \"on\"\r\n}\r\n]\r\n}"
}
$.ajax(settings).done(function (response) {
console.log(response);
});
}
做這個 -
function APIAWAY(){
var settings = {
"async": true,
"crossDomain": true,
"url": "https://api.smartthings.com/v1/devices/XXX-XXXX/commands",
"method": "POST",
"headers": {
"Authorization": "Bearer XXXX-XXXX-XXXX",
"Content-Type": "application/json",
"Accept": "*/*",
"Cache-Control": "no-cache",
"cache-control": "no-cache"
},
"processData": false,
"data": "{\r\n\"commands\": [\r\n{\r\n\"component\": \"main\",\r\n\"capability\": \"switch\",\r\n\"command\": \"on\"\r\n}\r\n]\r\n}"
}
$.ajax(settings).done(function (response) {
console.log(response);
});
}
問題是您正在添加服務器不接受的Postman-Token
標頭。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.