过去2天我试图解决我的问题。现在我需要帮助。我的任务非常简单。 Jquery ajax请求将用户名和密码发布到服务器,其中是python框架“Flask”。我做了一个非常简单的例子,但它仍然不起作用。ajax回调返回0
AJAX:
$.ajax({
url: "http://localhost:5000/login",
type: "POST",
dataType: "json",
data: values, //(json with username and password)
complete:function(response){
console.log(response);
}
});
我试图重写成功和错误的功能,但结果是一样的。 一切恢复:
Object { readyState=0, status=0, statusText="error", more...}
瓶实现:
@app.route("/login", methods=["GET", "POST"])
def login_page():
return jsonify(redirect="index.html")
页眉:
响应:
Content-Length 30
Content-Type application/json
Date Sun, 20 Oct 2013 09:50:19 GMT
Server Werkzeug/0.9.3 Python/2.7.3
Set-Cookie session=eyJfaWQiOnsiIGIiOiJOakkwTUdVM01HVXhNR0prWmpka05qQXlPRGcwT1dWbFlUQmhPRGRpT0RrPSJ9fQ.BUU42w.FDnVp15DxEsMPP_TsOpq8OzzU5I; HttpOnly; Path=/
要求:
Accept application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language cs,en-us;q=0.7,en;q=0.3
Cache-Control no-cache
Connection keep-alive
Content-Length 102
Content-Type application/x-www-form-urlencoded; charset=UTF-8
Host 127.0.0.1:5000
Origin http://localhost
Pragma no-cache
Referer http://localhost/login.html
User-Agent Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0
我试图返回重定向,并且控制台网络日志显示我返回,但我发现ajax不重定向(https://stackoverflow.com/a/17256609/2107985)。 每次来自ajax函数的console.log都显示我“错误”,虽然有200 OK。
通过手动输入链接:http://localhost:5000/login
到浏览器,它通常返回json。
没有警告。我将[这些](http://stackoverflow.com/a/9570852/2107985)数据添加到标题,但它没有帮助。 –
我使用这个[code](http://flask.pocoo.org/snippets/56/)。 –
响应标题现在说什么?响应主体是什么样的? – Quentin