為了從不記名令牌 API 獲取資料,我已經嘗試了 3 天,但我做不到
這是我的代碼:
const token = "MYOTKEN";
function App() {
const body = {
product: {
somerandombody,
},
};
axios
.get("http://upayments-studycase-api.herokuapp.com/api/products", body, {
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " token,
},
})
.then((response) => {
console.log("response", response.data);
})
.catch((error) => {
console.log("error", error.response);
});
這是我得到的錯誤:
error
{data: {…}, status: 401, statusText: 'Unauthorized', headers: AxiosHeaders, config: {…}, …}
config
:
{transitional: {…}, transformRequest: Array(1), transformResponse: Array(1), timeout: 0, adapter: ?, …}
data
:
{message: 'not authorized', stack: null}
headers
:
AxiosHeaders {content-length: '41', content-type: 'application/json; charset=utf-8', Symbol(defaults): null}
request
:
XMLHttpRequest {onreadystatechange: null, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}
status
:
401
statusText
:
"Unauthorized"
[[Prototype]]
:
Object
所以,我想要的是使用不記名令牌從 API 獲取資料。我究竟做錯了什么?
uj5u.com熱心網友回復:
嘗試使用 API 使用軟體(例如 Postman)測驗該端點,以便您可以查看服務器是否有故障。
uj5u.com熱心網友回復:
錯誤來自服務器。您需要重新檢查在服務器端訪問端點的邏輯。
uj5u.com熱心網友回復:
這可能有兩個原因:1.您使用的令牌可能已過期,或者 2.您無權訪問端點。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/534466.html
上一篇:根據自身和其他欄位設定一個欄位值
