Access to XMLHttpRequest at 'http://localhost:22222/api/Login/TestGet' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
通過錯(cuò)誤信息可以很清晰的定位到錯(cuò)誤(注意看標(biāo)紅部分)priflight說明是個(gè)預(yù)請(qǐng)求,CORS 機(jī)制跨域會(huì)首先進(jìn)行 preflight(一個(gè) OPTIONS 請(qǐng)求), 該請(qǐng)求成功后才會(huì)發(fā)送真正的請(qǐng)求。這一設(shè)計(jì)旨在確保服務(wù)器對(duì) CORS 標(biāo)準(zhǔn)知情,以保護(hù)不支持 CORS 的舊服務(wù)器通過錯(cuò)誤信息,我們可以得到是預(yù)檢請(qǐng)求的請(qǐng)求響應(yīng)頭缺少了 Access-Control-Allow-Origin,錯(cuò)哪里,我們改哪里就好了。修改Nginx配置信息如下(紅色部分為添加部分),缺什么就補(bǔ)什么,很簡(jiǎn)單明了
Access to XMLHttpRequest at 'http://localhost:22222/api/Login/TestGet' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
Access to XMLHttpRequest at 'http://localhost:22222/api/Login/TestGet' from origin 'http://localhost:8080' has been blocked by CORS policy: Request header field authorization is not allowed by Access-Control-Allow-Headers in preflight response.
There could be several add_header directives. These directives are inherited from the previous level if and only if there are no add_header directives defined on the current level.
Access to XMLHttpRequest at 'http://localhost:22222/api/Login/TestGet' from origin 'http://localhost:8080' has been blocked by CORS policy: Method PUT is not allowed by Access-Control-Allow-Methods in preflight response.
Access to XMLHttpRequest at 'http://localhost:22222/api/Login/TestGet' from origin 'http://localhost:8080' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple values '*, http://localhost:8080', but only one is allowed.