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

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

问题描述

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

<form id="uploadForm"><div class="form-group"><label for="exampleInputEmail1">电子邮件地址</label><input name="name" type="email" class="form-control" id="exampleInputEmail1" placeholder="Email">

<div class="form-group"><label for="exampleInputPassword1">密码</label><input name="password" type="password" class="form-control" id="exampleInputPassword1" placeholder="密码">

<button id="submit" type="button" class="btn btn-default">提交</button></表单><script src="../js/plugins/jquery-3.2.1.min.js?v=c9f5aeeca3"></script><script src="../js/common/bootstrap.min.js?v=5869c96cc8"></script><脚本>$(函数(){$('#submit').on('click',function(){$.ajax({url: 'http://www.example.com/login',方法:'POST',数据:$('#uploadForm').serialize(),内容类型:假,过程数据:假,缓存:假,成功:功能(数据){控制台日志(数据);if( 数据 === '错误'){alert('登录失败')}别的{location.href = 数据;}},错误:函数(jqXHR){警报('错误');}});});});

以及后端部分:

class LoginHandler(tornado.web.RequestHandler):def post(self, path):尝试:打印(self.request.body)name = self.get_body_argument("name")除了作为 e 的例外:打印(e)

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

<块引用>

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

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

解决方案

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

$.ajax({url: 'http://www.example.com/login',方法:'POST',数据:$('#uploadForm').serialize(),contentType: 'application/x-www-form-urlencoded',过程数据:假,...

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

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)

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: Bad Request (Missing argument name)

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

解决方案

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

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

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

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆