過去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