flask - Axios,对 Flask 的 POST 请求

标签 flask axios

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

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);
          });

现在是 Flask 的部分
...
data = request.get_json(silent=True)
item = {'label': data.get('label'), 'text': data.get('text')}
print item
...

但是,我最终会出现以下错误:

XMLHttpRequest 无法加载 http://127.0.0.1:5000/test .对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin” header 。起源' http://localhost:3000 ' 因此不允许访问。

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

这里的解决方案
from flask_cors import CORS, cross_origin
app = Flask(__name__)
cors = CORS(app, resources={r"/YOURAPP/*": {"origins": "*"}})

最佳答案

您需要为 Flask 应用程序添加 CORS 支持。在此处查看相关威胁:
Flask-CORS not working for POST, but working for GET .可以在此处找到 Flask 的流行 CORS 扩展:https://flask-cors.readthedocs.io/en/latest/ .

关于flask - Axios,对 Flask 的 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45373124/

相关文章:

python - 亚马逊负载均衡器的 flask 应用程序超时

python - 在python中接收并循环遍历json数据

python - 在需要可扩展的 Python Web 应用程序中解开大量数据是否可行?

python - flask/sqlalchemy - OperationalError : (sqlite3. OperationalError) 没有这样的表

javascript - 努克斯 : how do I access axios within the fetch() method?

python - Angular 路由器不使用 ng build 与 Flask 服务器交互

node.js - 通过 Fiddler 发送 NodeJs Axios 请求

javascript - 使用 Axios 检索然后将照片发布到 Foursquare Checkin

javascript - 错误: Request failed with status code 401 : Unauthorized in React. js并使用setState进行授权

vue.js - 我在 asyncData 中遇到非常慢的 Axios 请求