tornado post request: missing argument

為{幸葍}努か 提交于 2020-12-13 04:06:12

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/45415555/tornado-post-request-missing-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!