XMLHttpRequest:请求头字段postman-token在预检响应中未被Access-Control-Allow-Headers允许

我想通过访问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);
    });
}
点赞
用户2252182
用户2252182
函数APIAWAY(){
 var settings = {
          “异步”:true,
          “跨域”: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 \”component \“: \”main \“,\ r \ n \”capability \“: \”switch \“,\ r \ n \”command \“: \”on \“}] \ r \ n}”
        }

        $。Ajax(设置)已完成(响应){
          console.log(response);
        });
    }

问题在于您正在添加未被服务器接受的Postman-Token标头。

2019-08-06 22:30:42