简体   繁体   English

如何在axios Next js中修复“错误:请求失败,状态码为404”

[英]How to fix, “Error: Request failed with status code 404” in axios Next js

I am using next js when trying to call an API endpoint(dispatching a redux action)in getInitialProps. 我试图在getInitialProps中调用API端点(调度redux动作)时使用下一个js。 I am getting the 404 error, my /api/ is proxied in nginx server, all other routes work very well only this route is causing the problem. 我收到404错误,我的/ api /代理在nginx服务器中,所有其他路由工作得非常好,只有这条路由导致问题。

I have tried by changing the api fetch function call to async but still the same error. 我已经尝试通过将api fetch函数调用更改为异步但仍然是相同的错误。

static async getInitialProps({ store, query: { id } }) {
    await store.dispatch(fetchP(id));
    console.log('GetIntial');
    return { id };
 }

My action creator 我的动作创作者

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,
      });
    });
};

My nginx config 我的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;
  }

Error Response Error: Request failed with status code 404 at createError (/home/ubuntu/mainWebApp/node_modules/axios/lib/core/createError.js:16:15) at settle (/home/ubuntu/mainWebApp/node_modules/axios/lib/core/settle.js:18:12) at IncomingMessage.handleStreamEnd (/home/ubuntu/mainWebApp/node_modules/axios/lib/adapters/http.js:202:11) at IncomingMessage.emit (events.js:198:15) at endReadableNT (_stream_readable.js:1139:12) at processTicksAndRejections (internal/process/task_queues.js:81:17) 错误响应错误:请求失败,状态代码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.

相关问题 Axios - [未处理的承诺拒绝:错误:请求失败,状态码为 404] - Axios - [Unhandled promise rejection: Error: Request failed with status code 404] 错误:请求失败,状态码为 404,React,Axios - Error: Request failed with status code 404, React, Axios VueJS、expressJS、axios 删除错误:请求失败,状态码为 404 - VueJS, expressJS, axios DELETE error: Request failed with status code 404 反应 - axios.post 错误“请求失败,状态码 404” - React - axios.post error 'Request failed with status code 404' Axios GET 请求失败,状态码为 404 - Axios GET request failed with status code 404 错误:React JS 中的请求失败,状态代码为 401 axios - Error: Request failed with status code 401 axios in React JS 使用 axios 在本机反应中出现错误 404 - 请求失败,状态码为 404 - Error 404 in react native using axios - Request failed with status code 404 Axios 模拟适配器给出错误“错误:请求失败,状态码 404” - Axios mock adapter giving error 'Error: Request failed with status code 404' axios req failed 错误:在 XMLHttpRequest.handleLoad 结算时(settle.js:17),在 createError(createError.js:16)处请求失败,状态码为 404 - axios req failed Error: Request failed with status code 404 at createError (createError.js:16) at settle (settle.js:17) at XMLHttpRequest.handleLoad react-native axios.get() [错误:请求失败,状态码 404] - react-native axios.get() [Error: Request failed with status code 404]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM