【发布时间】:2014-06-29 23:24:07
【问题描述】:
我正在使用 Flask 运行服务器。这是我的views.py:
from flask import render_template
from app import app
@app.route('/')
@app.route('/user_form.html', methods=["GET", "POST"])
def index():
return render_template("user_form.html")
user_form.html 包含以下 Javascript:
<SCRIPT>
function get_UserInputValues(form) {
var getzipcode = document.getElementById('user_zip').value;
var getcuisine = document.getElementById('cuisine').value;
var selection1 = $("#slider1").slider("value");
var selection2 = $("#slider2").slider("value");
var selection3 = $("#slider3").slider("value");
var myurl = 'http://127.0.0.1:5000/mypython.py';
/*alert(getzipcode);
alert(getcuisine);
alert(selection1);
alert(selection2);
alert(selection3);*/
$('#myForm').submit();
$.ajax({url: myurl, type: "POST", data: {zip: getzipcode, cuisine:getcuisine}, dataType: 'json', done: onComplete})
}
function onComplete(data) {
alert(data);
};
</SCRIPT>
user_form.html 和 mypython.py 文件位于同一个“模板”目录下。但是,我收到消息“不允许使用方法。请求的 URL 不允许使用该方法”。
查看 Stackoverflow 上提出的类似问题,我确保在方法中包含“GET”和“POST”。为什么我仍然有这个错误?
作为测试,“mypython.py”如下:
def restaurant_choice(zipcode, cuisine):
print "zipcode:", zipcode
return "cuisine: ", cuisine
restaurant_choice(getzipcode, getcuisine)
【问题讨论】:
-
在日志中检查哪个动词到达服务器。它真的是一个帖子吗?有时客户端会先发送一个 OPTIONS,然后再发送 GET/POST。
-
这是我在终端登录时得到的结果:127.0.0.1 - - [29/Jun/2014 19:20:41] "POST / HTTP/1.1" 405 -
标签: javascript python flask