【发布时间】:2015-05-08 15:14:42
【问题描述】:
我有一个搜索按钮,点击该按钮后,应将数据发送到我的服务器。 javascript看起来像这样:
$('#search-btn').click(function() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/my_route",
data: JSON.stringify({title: 'hallo', article: 'test'}),
dataType: 'json',
success: function (data) {
console.log(data.title);
console.log(data.article);
},
});
});
在服务器上,我的路由如下所示:
@app.route('/my_route')
def route(methods=['GET', 'POST']):
print(request.method)
return flask.jsonify({"foo": "bar"})
使用此设置,我从 jQuery 收到控制台错误:POST http://localhost:5000/ 405 (METHOD NOT ALLOWED)
我不明白为什么这段代码会向错误的资源抛出 405 广告发布数据? ajax 和路由都指向/my_route。谁能发现我的问题?谢谢。
【问题讨论】:
标签: javascript jquery python flask