【问题标题】:Axios POST is not parsed correctly in Python FlaskAxios POST 在 Python Flask 中未正确解析
【发布时间】:2020-08-05 17:13:33
【问题描述】:

简介

您好,我可以看到这个问题被问了多次,但不是正确的解决方案。

怎么了

GET 请求按预期工作。但如果我使用 Vue.js 和 Axios 向烧瓶服务器发送 POST 请求。

使用 Postman 发送的 JSON POST 也可以使用。

这是 axios 调用:

async submit() {
      await this.$axios.post("/bob/posting", { foo: "bar" }).then(response => {
        console.log(response)
      })
}

在烧瓶一侧:

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

@app.route('/bob/posting', methods=['POST'])
def handleRoute():
    print(request.is_json)
    return (
        request.args
        or request.form
        or request.get_json(force=True, silent=True)
        or request.data
    )

烧瓶端的结果:

True会被打印出来(表示一个json请求)

不会返回任何内容(另外,对于每个选项,我都尝试单独打印以防万一)

如何在烧瓶服务器上正确使用从 axios json 请求接收的变量?

【问题讨论】:

  • 您的请求在 POST 中是否有任何错误?应该在 Chrome Inspector > Network to your API 中添加红线。或使用 Postman 查看问题所在(无论是从 Flask 还是 Vue 端)
  • 没有错误,只是挂起而不是打印json然后flask返回400

标签: python-3.x vue.js flask axios flask-cors


【解决方案1】:

完全不明白为什么,但添加文本/纯内容类型修复了它..什么...

async submit() {
      await this.$axios.post("/bob/posting", { foo: "bar" }, {'content-type':'text/plain'}).then(response => {
        console.log(response)
      })
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    相关资源
    最近更新 更多