0

When this AJAX request POSTS, the newUser() function says no arguments are being passed, even though i have the userInput and passInput fields filled out. The JS/JQ/AJAX:

            var userInput = document.getElementById('registerUsername');
            var passInput = document.getElementById('registerPassword');
            var message = document.getElementById('checkUsernameMessage');

            $(document).ready(function() {
                $('#submitRegisterButton').click(function () {
                    $.ajax({
                        type: "POST",
                        url: "/newUser",
                        data: JSON.stringify({"username":userInput, "password":passInput}),
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        async: true,
                        cache: false, 
                        success: function (msg) {
                            $('#checkUsernameMessage').text(msg.d);
                        }
                    });
                });
            });

And my python bottle function newUser() :

@post('/newUser')
def newUser(username, password):
    etc..

1 Answer 1

2

You need to nest your selectors within your dom ready call. Right now they are running before the DOM is ready, and are thus returning undefined. You can verify this by consoling the variables to see if they return any data.

The other thing, is you probably want to select the value of these inputs, and not return the DOM elements themselves: so instead, try

var userInput = document.getElementById('registerUsername').value etc.

        $(document).ready(function() {


            $('#submitRegisterButton').click(function () {

                var userInput = document.getElementById('registerUsername').value;
                var passInput = document.getElementById('registerPassword').value;
                var message = document.getElementById('checkUsernameMessage').value;

                $.ajax({
                    type: "POST",
                    url: "/newUser",
                    data: JSON.stringify({"username":userInput, "password":passInput}),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    async: true,
                    cache: false, 
                    success: function (msg) {
                        $('#checkUsernameMessage').text(msg.d);
                    }
                });
            });
        });

This should fix your issue.

With the clientside issue fixed, the python issue was:

The post request was being called as: def newUser( username, password ) where there should have been no arguments passed in, but derived from the form variable:

def newUser():
    username = request.forms.get('userInput')
    password = request.forms.get('passInput')
    message = request.forms.get('message')
Sign up to request clarification or add additional context in comments.

7 Comments

I corrected what you pointed out, and I am still getting the error: TypeError: newUser() takes exactly 2 arguments (0 given) 127.0.0.1 - - [15/Aug/2014 14:55:19] "POST /newUser HTTP/1.1" 500 1276 .
Are you importing your post and request from the bottle library? from bottle import post, request I think this is what's throwing the error.
I am importing everything from bottle, and the issue is still present :(
Can you post your python?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.