Axios,向Flask发出POST请求

编程入门 行业动态 更新时间:2024-10-26 23:41:26
本文介绍了Axios,向Flask发出POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我尝试使用axios对flask服务器进行POST:

I try to make a POST to a flask server using axios:

var config = { headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*'} } axios.post("127.0.0.1:5000/test", { label : "Test" , text : "Test"} , config ) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); });

现在是Flask的一部分

Now the part of Flask

... data = request.get_json(silent=True) item = {'label': data.get('label'), 'text': data.get('text')} print item ...

但是,我将遇到以下错误:

However, I ll end up with the following error:

XMLHttpRequest无法加载 127.0.0.1:5000/test .对预检请求的响应未通过访问控制检查:在所请求的资源上不存在"Access-Control-Allow-Origin"标头.因此,不允许访问来源" localhost:3000 ".

XMLHttpRequest cannot load 127.0.0.1:5000/test. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost:3000' is therefore not allowed access.

为什么?我会按照建议设置标题.

Why? I LL set the header as suggested.

解决方法

from flask_cors import CORS, cross_origin app = Flask(__name__) cors = CORS(app, resources={r"/YOURAPP/*": {"origins": "*"}})

推荐答案

您需要在Flask应用中添加CORS支持.在此处查看相关威胁: Flask-CORS不适用于POST,但为GET工作.可以在以下位置找到Flask的常用CORS扩展名: flask-cors.readthedocs. io/en/latest/.

You need to add CORS support to your Flask app. See a related threat here: Flask-CORS not working for POST, but working for GET. A popular CORS extension for Flask can be found here: flask-cors.readthedocs.io/en/latest/.

更多推荐

Axios,向Flask发出POST请求

本文发布于:2023-10-09 18:49:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1476489.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Axios   Flask   POST

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!