我正在嘗試在我的 Javascript React 應用程序中使用 Axios 發(fā)出一個簡單的 GET 請求。此請求與 Postman 完美配合,但我無法使用 Axios。我嘗試了幾種不同的代碼變體,但都不起作用。這是最簡單的版本: import React, { useEffect } from 'react'; import { Auth } from 'aws-amplify'; useEffect(() => { const fetchRoles = async () => { var authToken; Auth.currentSession() .then(data => { authToken = data.getAccessToken().getJwtToken(); }) .catch(err => console.log(err)); var config = { headers: { Authorization: "Bearer " + authToken} }; var bodyParameters = { key: "value" }; axios.get("http://127.0.0.1:5000/accounts/roles", bodyParameters, config) .then(response => { console.log(response); }) .catch(error => { console.log(error); }); fetchRoles(); }我每次得到的錯誤是:127.0.0.1 - - [19/Aug/2019 14:12:27] "GET /account_management/roles HTTP/1.1" 401 -Exception getting user An error occurred (InvalidParameterException) when calling the GetUser operation: 1 validation error detected: Value at 'accessToken' failed to satisfy constraint: Member must satisfy regular expression pattern: [A-Za-z0-9-_=.]+我不明白這個錯誤,因為我已經(jīng)根據(jù)這個 RegEx 表達式檢查了 Auth Token 并且它是有效的。
可以使用 Postman 訪問本地端點,但不能使用 Axios
躍然一笑
2021-09-17 12:58:32