0
當我從瓶視圖功能發回URL到我的javascript文件,沒有重定向發生,而是它返回一個Type error: 'NoneType' object is not subscriptable
。ajax響應瓶沒有重定向
我的看法功能:
@app.route('/login', methods=['POST','GET'])
def login():
if request.method == "POST":
print('check')
values = request.json
print(values)
result = db.session.query(Emp_Details).filter_by(ID=values['ID']).first()
print(result.Password)
if result.Password == values['Password'] and result.Department == 'HR':
print(jsonify(url=url_for('hr_department')))
return jsonify(url=url_for('hr_department'))
elif result.Password == values['Password'] and result.Department == 'DEVELOPMENT':
return url_for('devs')
else:
abort(401)
else:
return render_template("login.html")
我的Ajax請求:
$.ajax({
type :"POST",
url :"/login",
contentType: 'application/json;charset=UTF-8',
data : JSON.stringify(data, null, '\t'),
dataType : 'json',
success : function (data) {
window.location.href = data['url']
},
error : function (response) {
alert(response)
},
});
日誌在我的控制檯:
check
{'Password': 'leechan2', 'ID': '10090'}
leechan2
<Response 25 bytes [200 OK]>
127.0.0.1 - - [26/Dec/2016 23:34:37] "POST /login HTTP/1.1" 200 -
check
None
127.0.0.1 - - [26/Dec/2016 23:34:37] "POST /login HTTP/1.1" 500 -
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 2000, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1991, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1567, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.5/dist-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.5/dist-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/gautham/Intern-flask/Intern/App.py", line 24, in login
result = db.session.query(Emp_Details).filter_by(ID=values['ID']).first()
TypeError: 'NoneType' object is not subscriptable
127.0.0.1 - - [26/Dec/2016 23:34:37] "GET /login?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2016 23:34:37] "GET /login?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2016 23:34:37] "GET /login?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2016 23:34:37] "GET /login?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2016 23:34:37] "GET /login?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2016 23:34:37] "GET /login?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
使用'印刷(值)',看看你有什麼變量。它似乎是'沒有'。 – furas
現在我在您的日誌中看到'values'是'None' - 您可以在Chrome/Firefox中使用'DevTools'來查看當瀏覽器運行'$ .ajax'時發送的數據。 – furas
您轉換的數據的外觀JSON在'$ .ajax'中?如果我使用'data = {'ID':'foobar'};'然後'JSON.stringify(data,null,'\ t'),'正確地發送它。 – furas