簡體   English   中英

axios GET 請求更改為 Options 請求 - 狀態 401 未授權 - 郵遞員工作

[英]axios GET request changes to Options request - status 401 Unauthorized- Postman works

我知道整個“預檢請求”的故事......而且我的服務器確實支持選項請求。

困擾我的是,我嘗試了每一個在線 API 測試 {Postmamn,reqbin...} 並且它工作得很好,但是當我使用簡單的 axios 代碼或獲取時,我得到了狀態為 401 Unauthorized 的選項請求!

localhost env 在localtunnel上運行,我正在使用 next.js 默認服務器配置

https://enable-cors.org/server_nginx.html將 nginx 添加到服務器

/example.com/conf/nginx/cors.conf


**Axios 代碼**
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://example.com/xx/wp-json/x/api. (Reason: CORS preflight response did not succeed). Status code: 401.

控制台錯誤

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://example.com/xx/wp-json/x/api. (Reason: CORS preflight response did not succeed). Status code: 401.
 (Reason: CORS header 'Access-Control-Allow-Origin' does not match 'http://localhost:3000, http://localhost:3000')
服務器具有以下標頭
訪問控制允許憑據:true
access-control-allow-headers:授權、X-WP-Nonce、Content-Disposition、Content-MD5、Content-Type
訪問控制允許方法: OPTIONS 、GET、POST、PUT、PATCH、DELETE
訪問控制允許來源:https://localtunnel-URL.loca.lt
內容類型:應用程序/json; 字符集=UTF-8




在此處輸入圖像描述

AXIOS 請求方法更改為 'OPTIONS' 而不是 'GET'

我知道整個“預檢請求”的故事......而且我的服務器確實支持選項請求。

在允許訪問 OPTIONS 響應之前,您的服務器需要授權。

本質上,對話是這樣的:


瀏覽器:我可以提出包含我的憑據的請求嗎?

服務器:由於您在請求包含您的憑據的權限時沒有包含您的憑據,因此您無權包含您的憑據。


您需要從 authn/authz 中排除 OPTIONS 請求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM