12

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;
        }
    }
});
1
  • 1
    in python code you have typo, Write request.method=='post ': instead of request.method=='psot': Commented May 27, 2015 at 3:45

1 Answer 1

30

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.