gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:03:04 24 4
gpt4 key购买 nike

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

我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com