简体   繁体   中英

POST request from react native does not reach my flask server

I'm trying to send some data from my frontend (react native) to a flask application running on a remote computer. Something isn't working and I'm not sure where the problem lies.

The flask app is listening on port 5000 on the remote server. I've tried simply sending a post request to the IP address of the server (with the port), but this doesn't work.

On my frontend I have this code:

          fetch('http://18.222.109.76:5000/add_New_Entry', {
               method: 'POST',
               headers: {
                   Accept: 'application/json',
                   'Content-Type': 'application/json',
               },
               body: JSON.stringify({
                   firstParam: 'yourValue',
                   secondParam: 'yourOtherValue',
               }),
           }).then((response) => /*do something with response*/
           ));

On my backend I have this code as part of the flask app:

app = Flask(__name__)
app.debug = True

app.config['CORS_HEADERS'] = 'Content-Type'
CORS(app)

@app.route("/")
def hello():
    return ""

@app.route('/add_New_Entry', methods=['GET', 'POST'])
@cross_origin(origin='*',headers=['Content- Type','Authorization'])
def add_New_Form():
    print('bla')
    return ""

if __name__ == '__main__':
    app.run(debug=True)

So, I expect that if I run the piece of code from the frontend, the server will print "bla", but it doesn't, and no sign of failure appears on the frontend as well (maybe it's because I failed to catch the exception?)

So I finally figured it out. Apparently it was because I was using a remote Amazon server, and I had to open its ports on the amazon interface.

After I did, this worked flawlessly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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