I am currently learning Flask.
After sending data through $.ajax()
with jQuery, type='post'
the server side gives an error when I check request.method
. The same occurs with type='get'
.
error
builtins.ValueError
ValueError: View function did not return a response
Traceback (most recent call last)
File "C:\Python33\lib\site-packages\flask\app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python33\lib\site-packages\flask\app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "C:\Python33\lib\site-packages\flask\app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python33\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Python33\lib\site-packages\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python33\lib\site-packages\flask\app.py", line 1478, in full_dispatch_request
response = self.make_response(rv)
File "C:\Python33\lib\site-packages\flask\app.py", line 1566, in make_response
raise ValueError('View function did not return a response')
ValueError: View function did not return a response
python code
if request.method=='psot': #**Error occur when i check**
req = request.get_json()
ck=query_db("select * from users where username=?",[req['u']])
if ck:
if check_password_hash(ck[0]['password'],req['p']):
session['username']=req['u']
return jsonify({'result':'1234'})
else:
return jsonify({'result':'noo'})
else:
return jsonify({'result':'noo'})
jquery
$.ajax({
type:'post',
contentType: 'application/json',
url:"/login",
data:JSON.stringify({'u':luser.val(),'p':lpass.val()}),
dataType:'json',
success:function(data)
{
if(data.result=='1234')
{
window.location.href='/profile';
}
else
{
$('#log3s').html('username or password not correct').css('color','red');
return false;
}
}
});
You need to make sure you registered your view route with the right methods:
@app.route('/login', methods=['GET', 'POST'])
to allow both GET
and POST
requests, or use:
@app.route('/login', methods=['POST'])
to allow only POST
for this route.
To test for the request method, use uppercase text to test:
if request.method == 'POST':
but make sure you always return a response. if request.method
is not equal to 'POST'
you still need to return a valid response .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.