简体   繁体   English

在烧瓶中检查 request.method 时出错

[英]Error when check request.method in flask

I am currently learning Flask.我目前正在学习 Flask。

After sending data through $.ajax() with jQuery, type='post' the server side gives an error when I check request.method .使用 jQuery 通过$.ajax()发送数据后, type='post'在我检查request.method时服务器端给出错误。 The same occurs with type='get' . 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:允许GETPOST请求,或使用:

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

to allow only POST for this route.允许POST这条路线。

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 .如果request.method等于'POST'你仍然需要返回一个有效的 response

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DJango 不执行 request.method == "post" 与 ajax 数据提交 - DJango doesn't execute request.method == "post" with ajax data submission Flask-表单提交时出现错误的请求错误 - Flask - Bad Request Error on Form Submit Flask 和 Ajax 发布 HTTP 400 错误请求错误 - Flask and Ajax Post HTTP 400 Bad Request Error 了解对Flask端点的AJAX发布请求期间的400错误 - Understanding 400 error during AJAX Post request to Flask endpoint Flask-使用request.form.get时获取NoneType - Flask - Getting NoneType when using request.form.get 使用jQuery向PHP发送PUT AJAX请求时错误405方法不允许 - Error 405 Method Not Allowed when sending a PUT AJAX request to PHP with jQuery 向 Spring 发送 PUT AJAX 请求时不允许使用错误 405 方法 使用 jQuery 启动? - Error 405 Method Not Allowed when sending a PUT AJAX request to Spring Boot with jQuery? CORS错误:尝试访问Azure Search Api时,对预检请求的响应未通过访问控制检查 - CORS Error:Response to preflight request doesn't pass access control check when trying to access Azure Search Api jquery ajax 将请求发布到 flask 错误,但与获取请求完美配合,为什么? 编辑:未能加载响应数据 - jquery ajax post request to flask error, but works perfectly with get request why? EDIT: failed to load response data 服务器中的Ajax请求的405方法不允许错误 - 405 Method Not Allowed Error for Ajax Request in Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM