简体   繁体   English

Axios Expo 中的请求 React Native 应用程序返回意外的 405 错误

[英]Axios request in Expo React Native app returns unexpected 405 error

I'm developing a React Native app with Expo and I'm using Axios to try sending http requests.我正在使用 Expo 开发 React Native 应用程序,我正在使用 Axios 尝试发送 http 请求。 Unfortunately, for every request I get a 405 status response.不幸的是,对于每个请求,我都会收到 405 状态响应。

This is the axios instance I've created to manage the requests (API url replaced with dummy for security reasons):这是我为管理请求而创建的 axios 实例(出于安全原因,API url 已替换为虚拟对象):

    import axios from 'axios';

    const instance = axios.create({
        baseURL: 'https://actualapi.com',
        headers: {
            ['Content-Type']: 'application/json',
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Methods': '*'
        }
    });

    export default instance;

The actual request (in a redux thunk function aka async action creator):实际请求(在 redux thunk function aka async action creator 中):

    axios
        .post(
            requestPath,
            // { withCredentials: true },
            { email: email }
        )
        .then((res) => {
            console.debug(JSON.stringify(res));
            checkEmailSuccess(res);
        })
        .catch((error) => {
            console.debug(error);
            checkEmailFail(error);
        });

The error for every single request:每个请求的错误:

Request failed with status code 405
at node_modules\axios\lib\core\createError.js:16:14 in createError
at node_modules\axios\lib\core\settle.js:17:22 in settle
at node_modules\axios\lib\adapters\xhr.js:66:12 in onloadend
at node_modules\event-target-shim\dist\event-target-shim.js:818:20 in EventTarget.prototype.dispatchEventat node_modules\react-native\Libraries\Network\XMLHttpRequest.js:614:6 in setReadyState
at node_modules\react-native\Libraries\Network\XMLHttpRequest.js:396:6 in __didCompleteResponse
at node_modules\react-native\Libraries\vendor\emitter\_EventEmitter.js:135:10 in EventEmitter#emit       
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:414:4 in __callFunction
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:113:6 in __guard$argument_0
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:365:10 in __guard
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:112:4 in callFunctionReturnFlushedQueue

It was a wrong call.这是一个错误的电话。 I was doing a POST instead of GET.我正在做一个 POST 而不是 GET。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM