javascript - 405 : Method not allowed (AJAX query to Flask)

标签 javascript jquery python ajax flask

我看到了很多关于这个的问题,但我无法调试它。

我的 ajax 查询是这样的:

    function add_to_pipeline(){
        // console.log("In function");
        var mySelect = document.getElementById("select_list");
        var operator = mySelect.options[ mySelect.selectedIndex ].label;
        // console.log(mySelect.options[ mySelect.selectedIndex ].value);
        $("#pipeline_list").append('<li> '+ operator +'</li>');
        submit_to_server();
    }

    function submit_to_server(){
        var img = new FormData();
        img.append('file',$("#preview_list li:last-child")[0]);

        // Debugging statement
        //$("#preview_list li:last-child").dialog();
            $.ajax({
            type : "POST",
            url : "/_process",
            data : img,
            contentType: false,
            cache: false,
            processData: false,
            success: function(data) {
                alert(data['result']);
            }
        });
    }

HTML 是:

   <select id="select_list">
             {% for l in list %}
                 <option value={{ l.index }}>{{ l.name }}</option>
             {% endfor %}
   </select>
   <input type="button" id="p_submit" value="Add to pipeline" onclick="add_to_pipeline()">

flask 文件:

@app.route('/_process')
def process():
    img = request.files['file']
    return jsonify(result="Image received")

我要走最后一个<li> <ul> 的元素作为输入文件(图像)并通过 AJAX 发送。

最佳答案

您需要指定 View 允许的方法(默认为 GET),例如:

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

我认为在你的情况下你不需要 GET,所以简单:

@app.route('/_process', methods=['POST'])
def process():
    img = request.files['file']
    return jsonify(result="Image received")

这是文档中的示例: http://flask.pocoo.org/docs/0.10/quickstart/#http-methods

关于javascript - 405 : Method not allowed (AJAX query to Flask),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35757279/

相关文章:

javascript - 在每次循环迭代上添加延迟

python - 使用 Matplotlib 绘制正态分布

python - 将用户构建的 json 编码器传递给 Flask 的 jsonify

javascript - React 通过 onclick 调用组件

javascript - 如何在数据表的每一行上添加按钮?

javascript - 如何将一个 div 覆盖在一个 img 上,并在鼠标悬停时显示另一个 div?

javascript - 未键入以编程方式设置的值时跨度溢出

python - 使用 RPy2 和 dplyr 时为 "Error while parsing the string"

javascript - 使用 jquery 隐藏多个选定的行

Javascript On 模糊事件