【问题标题】:Flask, Axios POST returns a Network errorFlask,Axios POST 返回网络错误
【发布时间】:2019-05-06 06:23:06
【问题描述】:

我正在尝试让我的烧瓶 api 工作,但当我在 POST 中提交我的 axios 表单时仍然出现一些“网络错误”。

认为这是一个 CORS 错误,但我认为我已尽我所能让它工作。如果有人有想法。

API

from flask_cors import CORS, cross_origin

    UPLOAD_FOLDER = './WorkingDirectory/'
    ALLOWED_EXTENSIONS = set(['mp3', 'wav'])

    app = Flask(__name__)
    cors = CORS(app, resources={r"/*": {"origins": "*"}})

    @app.route('/', methods=['GET', 'POST'])
    @cross_origin()

我的 axios 帖子:

    onSubmit () {
        this.axios({
        method: 'post',
        url: 'http://127.0.0.1:5000/',
        data: {
            file: this.form.file,
            reverb: 'reverb',
        },
        headers: {
            'Content-Type': 'text/plain;charset=utf-8',
        },
    }).then(function (response) {
        console.log(response);
    }).catch(function (error) {
        console.log(error);
    });
}

确切的错误:

Login:  Error: Network Error
    at createError (createError.js?2d83:16)
    at XMLHttpRequest.handleError (xhr.js?b50d:87)

【问题讨论】:

  • 你能添加确切的错误吗
  • @mmenschig 添加了它
  • 问题中引用的错误信息与CORS无关
  • 你的 Flask 服务真的在运行吗?在 5000 端口上?
  • @Phil 我的烧瓶正在端口 5000 上运行,是的,看起来很奇怪的问题(从我在 Chrome 中看到的,是它正在发送一个 get)。它与邮递员和招摇的人完美配合。

标签: vue.js flask axios


【解决方案1】:

尝试从 axios 调用 post 并将 {} 放置在数据和标题周围。

     onSubmit () {
        const var path = 'http://127.0.0.1:5000/'
        this.axios.post({
        path,
        {data: {
            file: this.form.file,
            reverb: 'reverb'
        }},
        {headers: {
            'Content-Type': 'text/plain;charset=utf-8'
        }},
    }).then(function (response) {
        console.log(response);
    }).catch(function (error) {
        console.log(error);
    });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    • 2021-10-30
    • 2021-01-13
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多