![](/img/trans.png)
[英]How to pass state/data from one component to another in React.js (riot api specifically)
[英]How to pass data in body in get type Api in react.js?
我正在嘗試在 react.js 應用程序中傳遞獲取類型 Api 的正文中的數據。 我正在使用以下代碼。 但是 Api 沒有得到任何數據。
getUnits = ( key, text, code, limit, offset ) => {
let data = JSON.stringify( { unit: { key, text, code, limit, offset } } );
let config = {
method: 'get',
url: BaseURL + 'unit',
headers: { 'Content-Type': 'application/json' },
data: data
};
axios( config ).then( res => {
store.dispatch( {
type: GET_UNIT,
payload: res.data.units
} )
} ).catch( err => {
console.log(err);
})
})
}
添加@Jayna 評論的內容,您無法發送帶有獲取請求的正文。 您可以在 Postman 上執行此操作並為其生成 axios 代碼,但由於 ZDA88B4E490C2359B639728A136946C0C2919DE9B9ED780DEE2 有,因此無法正常工作。 默認情況下,在獲取請求中忽略正文
1您需要像這樣傳遞參數:
let config = {
method: 'get',
url: BaseURL + 'unit',
headers: { 'Content-Type': 'application/json' },
params: {
field1: 'field1',
field2: 'field2'
}
};
所以我的建議是在后端更改您的 url 以接受查詢參數並發送 axios 獲取這樣的請求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.