Flask - Ajax POST 请求 HTTP 400 错误请求

时间:2021-03-18 13:45:58

标签: python ajax flask

我想发送文件图像和分类,但我的帖子网址“预测”错误请求。 我的 HTML 代码;

<form id="upload-file" method="post" enctype="multipart/form-data">
        <input type="file" accept="image/*" name="image" id="input-file" style="display: none;">
        <label for="input-file" class="btn btn-lg btn-default" style="margin:10px;">Resim Yükle</label>
      </form>
      <button type="button" class="btn btn-lg btn-default " style="margin:10px;" id="analyze">Analiz Et</button> 

我的 Javascript 代码

var form_data = new FormData($('#upload-file')[0]);
        $.ajax({
            type: 'POST',
            url: '/predict',
            data: form_data,
            contentType: false,
            cache: false,
            processData: false,
            async: true,
            success: function (data) {
                console.log("Fonksiyon başarıyla çalıştı:" + data);
                $('.btn,.btn-lg,.btn-default').show();
                $("#style_file").remove();
            },
        });

我的python代码

@image_review_tool.route('/predict', methods=['GET', 'POST'])
def upload():
if request.method == 'POST':
    print(request)
    # Get the file from post request
    f = request.files['file']
    print(str(f))

只需使用ajax发送图像文件。我的经典化算法已经准备好了。但是 ajax 以 400 个错误的请求请求一个未知的原因。请帮忙!!!

0 个答案:

没有答案