Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when check request.method in flask

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;
        }
    }
});
like image 879
iammehrabalam Avatar asked May 31 '14 21:05

iammehrabalam


People also ask

How do I fix Error 405 Flask?

To fix POST Error 405 Method Not Allowed with Flask Python, we should make sure the action attribute of the form is set to the URL of the view that accepts POST requests. to create the template view. to add a form that has the action attribute set to the URL for the template view that we get with url_for('template') .

How do you check if method is POST or get in Flask?

Inside the view function, you will need to check if the request method is GET or POST. If it is a GET request, you can display the form. Otherwise, if it is a POST request, then you will want to process the incoming data.

How do you catch errors in Flask?

This can be done by registering error handlers. When Flask catches an exception while handling a request, it is first looked up by code. If no handler is registered for the code, Flask looks up the error by its class hierarchy; the most specific handler is chosen.


1 Answers

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.

like image 116
Martijn Pieters Avatar answered Oct 17 '22 15:10

Martijn Pieters