Python 如何从react本机应用程序联系flask restful api?

Python 如何从react本机应用程序联系flask restful api?,python,react-native,axios,flask-restful,Python,React Native,Axios,Flask Restful,我是后端开发的新手。我尝试开发一个restful api。我遵循文档并制作了建议的最小api,即返回jsonified dict。使用postman、curl和浏览器,没问题,api正在运行并响应我的请求 然而,从我的react本机应用程序中,我总是收到一个Netwrok错误 我尝试了很多东西: -flask serveur的多个IP地址 -使用axios的不同方式 -不同的操作系统:win10、ubuntu -不同的端点:/,/api,/test -编写api flask restful类、f

我是后端开发的新手。我尝试开发一个restful api。我遵循文档并制作了建议的最小api,即返回jsonified dict。使用postman、curl和浏览器,没问题,api正在运行并响应我的请求

然而,从我的react本机应用程序中,我总是收到一个Netwrok错误

我尝试了很多东西: -flask serveur的多个IP地址 -使用axios的不同方式 -不同的操作系统:win10、ubuntu -不同的端点:/,/api,/test -编写api flask restful类、flask应用程序函数的不同方法。。。 -网络文档中的manips:我的设备的开发模式,chrome开发工具端口转发 -我让一个react本地开发人员检查我的客户端代码,他说没什么问题

精度: -python代码在conda虚拟环境下运行 -appli在节点js和expo下运行 -我的应用程序和我的api的容器文件夹处于同一级别 -flask服务器没有响应404或其他任何内容,它根本没有响应,react native中返回的错误是网络错误 -根据url,网络错误会立即发生或延迟2分钟后发生 -当邮递员、curl、chrome调用flask时,它会显示请求和状态,但当我从react本机应用程序中按下按钮时,它不会做出反应

下面是我的一段python代码:

from flask import (Flask, jsonify)
# from flask_restful import Resource, Api
from flask_cors import CORS, cross_origin

app = Flask(__name__)
CORS(app)

@app.route("/api", methods=["GET"])
def get():
    return jsonify(hello='bouh !')

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=5000, debug=True)
以下是客户端代码:

import React, {Component} from 'react';
import { View, Button } from 'react-native';
import axios from 'axios'; 

import { sharedStyles } from '../../SHARED/_shared';

var url = "http://192.168.1.16:5000/api";

export default class PrevCommune extends Component {
    constructor(props){
        super(props);
        this.navigation=this.props.navigation;
    };

    getAxios=()=>{
        axios.get(`${url}`).then((response)=>{
            console.log("succes axios :",response);
        }).catch((error)=>{
            console.log("fail axios :", error);
        });
    };

    getFetch=()=>{
        fetch(url).then((response)=>{
            console.log("succes fetch :",response)
        }).catch((error)=>{
            console.log("fail fetch :",error)
        })
    }

    render(){
        return (
            <View style={sharedStyles.mainContainer}>
                <Button onPress={()=>this.getAxios()} title={"get axios"}></Button>

                <Button onPress={()=>this.getFetch()} title={"get fetch"}></Button>
            </View>
        );
    };
};

我看了很多关于FlaskAPI的短裙、视频和文章,但我没有发现我错在哪里。如果你有什么想法,请告诉我!我认为客户端和服务器代码都可以,问题似乎是我的请求被什么东西阻止了。

解决了:问题是我的防火墙。。。感谢ricardo提供的CORS文档=

您好,您是否使用post man验证了端点?当我在postman中输入ip地址时,它会给出正确的答案您是否在服务器端配置了CORS?哦,不,我不知道哦,我在我找到的一些文件中看到了cors,但我没有试过!谢谢你,我会试试看,不会再有结果了。但是COR是必需的,它比以前更好了=
fail axios : [Error: Network Error]
fail fetch : [TypeError: Network request failed]