龙卷风发布请求:缺少参数 [英] tornado post request: missing argument

查看:66
本文介绍了龙卷风发布请求:缺少参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用龙卷风构建一个Web服务器,现在正在创建一个登录模块.这是代码:

I'm using tornado to build a web server and now I'm creating a login module. Here is the code:

<body>
    <form id="uploadForm">
      <div class="form-group">
        <label for="exampleInputEmail1">Email address</label>
        <input name="name" type="email" class="form-control" id="exampleInputEmail1" placeholder="Email">
      </div>
      <div class="form-group">
        <label for="exampleInputPassword1">Password</label>
        <input name="password" type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
      </div>
      <button id="submit" type="button" class="btn btn-default">Submit</button>
    </form>
</body>
    <script src="../js/plugins/jquery-3.2.1.min.js?v=c9f5aeeca3"></script>
    <script src="../js/common/bootstrap.min.js?v=5869c96cc8"></script>
    <script>
        $(function(){
            $('#submit').on('click',function(){
                $.ajax({
                    url: 'http://www.example.com/login',
                    method: 'POST',
                    data: $('#uploadForm').serialize(),
                    contentType: false,
                    processData: false,
                    cache: false,
                    success: function(data) {
                        console.log(data);
                        if( data === 'ERROR'){
                            alert('login failed')
                        }else{
                            location.href = data;
                        }
                    },
                    error: function (jqXHR) {
                        alert('ERROR');
                    }
                });
            });
        });
    </script>

和后端部分:

And the backend part:

class LoginHandler(tornado.web.RequestHandler):
    def post(self, path):
        try:
            print(self.request.body)
            name = self.get_body_argument("name")
        except Exception as e:
            print(e)

进行测试时,我可以看到 print(self.request.body)给了我结果: b'name = test& password = tttt'但是之后我得到一个例外:

When I do a test, I can see that print(self.request.body) gives me the result: b'name=test&password=tttt' but after that I get an exception:

HTTP 400:错误的请求(缺少参数名称)

HTTP 400: Bad Request (Missing argument name)

name = test 只是在http正文中,但是为什么它告诉我缺少参数名称?

name=test is just in the http body but why it tells me missing argument name?

推荐答案

Tornado仅支持将"application/x-www-form-urlencoded"和"multipart/form-data"作为内容类型.因此,当我们向服务器发送发布请求时,我们必须使用正确的contentType发送请求.例如,

Tornado only supports "application/x-www-form-urlencoded" and "multipart/form-data" as content type. So when we send a post request to the server, we must send the request with the right contentType. For example,

$.ajax({
            url: 'http://www.example.com/login',
            method: 'POST',
            data: $('#uploadForm').serialize(),
            contentType: 'application/x-www-form-urlencoded',
            processData: false,
            ...

此外,由于默认设置为'application/x-www-form-urlencoded',因此我们可以忽略ajax中的 contentType .

Also, we can neglect the contentType in ajax as 'application/x-www-form-urlencoded' is set by default.

这篇关于龙卷风发布请求:缺少参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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