Axios, POST request to Flask -
i try make post flask server using axios:
var config = { headers: { 'content-type': 'application/json', 'access-control-allow-origin': '*'} } axios.post("http://127.0.0.1:5000/test", { label : "test" , text : "test"} , config ) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); });
now part of flask
... data = request.get_json(silent=true) item = {'label': data.get('label'), 'text': data.get('text')} print item ...
however, ll end following error:
xmlhttprequest cannot load http://127.0.0.1:5000/test. response preflight request doesn't pass access control check: no 'access-control-allow-origin' header present on requested resource. origin 'http://localhost:3000' therefore not allowed access.
why? ll set header suggested.
here solution
from flask_cors import cors, cross_origin app = flask(__name__) cors = cors(app, resources={r"/yourapp/*": {"origins": "*"}})
you need add cors support flask app. see related threat here: flask-cors not working post, working get. popular cors extension flask can found here: https://flask-cors.readthedocs.io/en/latest/.
Comments
Post a Comment