簡體   English   中英

React Axios,無法讀取 axios 響應中的 Set-Cookie 標頭

[英]React Axios, Can't read the Set-Cookie header on axios response

我無法在 axios 中讀取響應對象上的 set-cookie 標頭,我將用戶/密碼發布到后端,並且響應在 Set-Cookie 標頭中具有授權 cookie,但是我在 res 上看不到它.header 對象,但是如果我使用 chrome 開發工具並打開網絡選項卡,我可以看到響應標頭上的 cookie。

有什么線索嗎?

我的帖子: axios.post( endPointLogin, { email, password }, { withCredentials: true } )

控制台中的 res 對象

帶有設置 cookie 標頭的 chrome 開發工具上的標頭

export function login(email, password) {
  return http.post(
    endPointLogin,
    { email, password },
    { withCredentials: true }
  );
}

    const handleSignIn = async ( e ) => {
        e.preventDefault();
        const respuesta = await login( cuenta.email, cuenta.password );
        console.log( "res:", respuesta );
    };

回復:

config: {url: "http://localhost:3000/usuario/auth", method: "post", data: "{"email":"admin@bmw.com","password":"admin"}", headers: {…}, transformRequest: Array(1), …}
data: "OK"
headers:
access-control-allow-credentials: "true"
access-control-allow-headers: "Origin, X-Requested-With, Content-Type, Accept"
access-control-allow-origin: "http://localhost:5000"
connection: "close"
content-length: "2"
content-type: "text/plain; charset=utf-8"
date: "Tue, 03 Nov 2020 15:00:46 GMT"
etag: "W/"2-nOO9QiTIwXgNtWtBJezz8kv3SLc""
vary: "Accept-Encoding"
x-powered-by: "Express"
__proto__: Object
request: XMLHttpRequest {__sentry_xhr__: {…}, readyState: 4, timeout: 0, withCredentials: true, onreadystatechange: ƒ, …}
status: 200
statusText: "OK"
__proto__: Object

**谷歌開發工具網絡選項卡中的響應頭**

access-control-allow-credentials: true
access-control-allow-headers: Origin, X-Requested-With, Content-Type, Accept
access-control-allow-origin: http://localhost:5000
connection: close
content-length: 2
content-type: text/plain; charset=utf-8
date: Tue, 03 Nov 2020 15:00:46 GMT
etag: W/"2-nOO9QiTIwXgNtWtBJezz8kv3SLc"
set-cookie: jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbXByZXNhIjoiYm13IiwiZW1haWwiOiJhZG1pbkBibXcuY29tIiwic2VndXJpZGFkIjoiYWRtaW4iLCJpYXQiOjE2MDQ0MTU2NDZ9.CpIIScbZQCKsyxvc64CJ290fNCUlpKxZ5zBT3JK2tvc; Path=/
Vary: Accept-Encoding
X-Powered-By: Express

由於您在此請求中有 CORS,因此您必須授權要讀取的標頭此外,cookie 必須不是“httpOnly”,但似乎是這種情況有關CORS 的更多信息

您無法在 JavaScript 代碼中讀取Set-Cookie標頭,如這篇mdn文章中所述。

暫無
暫無

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

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