检查烧瓶中的request.method时出错 [英] Error when check request.method in flask

查看:103
本文介绍了检查烧瓶中的request.method时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习Flask.

I am currently learning Flask.

使用jQuery通过$.ajax()发送数据后,type='post'当我检查request.method时,服务器端给出了错误. type='get'也是如此.

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'.

错误

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代码

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'])

允许GETPOST请求,或使用:

@app.route('/login', methods=['POST'])

仅允许 POST用于此路由.

要测试请求方法,请使用大写文本进行测试:

To test for the request method, use uppercase text to test:

if request.method == 'POST':

但请确保您始终返回回复.如果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.

这篇关于检查烧瓶中的request.method时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆