【问题标题】:Why my request works with postman but not with vue.js axios?为什么我的请求适用于邮递员,但不适用于 vue.js axios?
【发布时间】:2020-09-14 23:29:22
【问题描述】:

我用 Flask 做了一个背面,用 vue.js 做了一个正面,

为什么我用邮递员发出请求,它返回我想要的,但不是用 axios ...

例如:

    this.$axios
      .post('http://127.0.0.1:5000/getUserDataByMail', { mail: 'test@test.com' })
      .then(response => {
        console.log('this.userData')
        console.log(response.data)
        this.userData = response
        }
      )

被处理:

@app.route('/getUserDataByMail', methods = ['GET', 'POST'])
def getUserDataByMail():
    args = request.args
    mail = args['mail']
    return jsonify(mail)
    cur = mysql.connection.cursor()
    dataCur = cur.execute('select * from userdata where email like "' + mail + '"')
    if dataCur > 0:
        data = cur.fetchall()
        cur.close()
        return jsonify(data)
    cur.close()

但这会导致错误 400 ...

POSThttp://127.0.0.1:5000/getUserDataByMail [HTTP/1.0 400 BAD REQUEST 4ms] 未捕获(承诺中)错误:请求失败,状态码为 400

帮帮我,我快疯了! (:

【问题讨论】:

  • 你为你的烧瓶项目配置了CORS吗?

标签: vue.js flask axios


【解决方案1】:

Axios 默认发布 application/json 请求正文。

要在 Flask 中读取 JSON 有效负载,请使用 request.json

content = request.json
mail = content["mail"]

我只能假设 Postman 有效,因为您发布了 application/x-www-form-urlencoded 请求正文或使用 URL 查询参数。

为了匹配您在 Axios 中所做的事情,请确保您发布 JSON

【讨论】:

    猜你喜欢
    • 2019-07-03
    • 1970-01-01
    • 2021-10-06
    • 2020-11-08
    • 2020-11-20
    • 2021-05-24
    • 2020-06-13
    • 2022-07-08
    • 2019-08-25
    相关资源
    最近更新 更多