[英]Axios - [Unhandled promise rejection: Error: Request failed with status code 404]
[英]How to fix, “Error: Request failed with status code 404” in axios Next js
我試圖在getInitialProps中調用API端點(調度redux動作)時使用下一個js。 我收到404錯誤,我的/ api /代理在nginx服務器中,所有其他路由工作得非常好,只有這條路由導致問題。
我已經嘗試通過將api fetch函數調用更改為異步但仍然是相同的錯誤。
static async getInitialProps({ store, query: { id } }) {
await store.dispatch(fetchP(id));
console.log('GetIntial');
return { id };
}
我的動作創作者
export const fetchp = id => async dispatch => {
dispatch({ type: FETCH_P_DETAILS_STARTED });
await API.get(`ps/${id}`)
.then(res => {
console.log(res);
dispatch({
type: FETCH_P_DETAILS_SUCCESS,
payload: res.data.data,
});
})
.catch(err => {
console.log(err);
if (!err.response) {
// network error
err.message = 'Error: Network Error';
console.log(err.message);
} else if (err.code === 'ECONNABORTED') {
console.log('Timeout');
} else {
err.message = err.message;
}
dispatch({
type: FETCH_P_DETAILS_FAILURE,
payload: err.message,
});
});
};
我的nginx配置
location /api/ {
proxy_pass http://127.0.0.1:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
錯誤響應錯誤:請求失敗,狀態代碼404在setupError(/home/ubuntu/mainWebApp/node_modules/axios/lib/core/createError.js:16:15)在結算時(/ home / ubuntu / mainWebApp / node_modules / axios / lib / core / settle.js:18:12)IncomingMessage.handleStreamEnd(/home/ubuntu/mainWebApp/node_modules/axios/lib/adapters/http.js:202:11)IncomingMessage.emit(events.js:198) :15)在processTicksAndRejections的endReadableNT(_ stream_readable.js:1139:12)(internal / process / task_queues.js:81:17)
所以問題是getInitialProps在服務器中執行而axios無法運行服務器使用https://www.npmjs.com/package/isomorphic-fetch 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.