Fix a 404: missing parameters error from a GET request to CherryPy
- by norabora
I'm making a webpage using CherryPy for the server-side, HTML, CSS and jQuery on the client-side. I'm also using a mySQL database.
I have a working form for users to sign up to the site - create a username and password. I use jQuery to send an AJAX POST request to the CherryPy which queries the database to see if that username exists. If the username exists, alert the user, if it doesn't, add it to the database and alert success.
$.post('submit', postdata, function(data) {
alert(data);
});
Successful jQuery POST.
I want to change the form so that instead of checking that the username exists on submit, a GET request is made as on the blur event from the username input. The function gets called, and it goes to the CherryPy, but then I get an error that says: HTTPError: (404, 'Missing parameters: username').
$.get('checkUsername', getdata, function(data) {
alert(data);
});
Unsuccessful jQuery GET.
The CherryPy:
@cherrypy.expose
def submit(self, **params):
cherrypy.response.headers['Content-Type'] = 'application/json'
e = sqlalchemy.create_engine('mysql://mysql:pw@localhost/6470')
c = e.connect()
com1 = "SELECT * FROM `users` WHERE `username` = '" + params["username"] + "'"
b = c.execute(com1).fetchall()
if not len(b) > 0:
com2 = "INSERT INTO `6470`.`users` (`username` ,`password` ,`website` ,`key`) VALUES ('"
com2 += params["username"] + "', MD5( '" + params["password"] + "'), '', NULL);"
a = c.execute(com2)
c.close()
return simplejson.dumps("Success!")
#login user and send them to home page
c.close()
return simplejson.dumps("This username is not available.")
@cherrypy.expose
def checkUsername(self, username):
cherrypy.response.headers['Content-Type'] = 'application/json'
e = sqlalchemy.create_engine('mysql://mysql:pw@localhost/6470')
c = e.connect()
command = "SELECT * FROM `users` WHERE `username` = '" + username + "'"
a = c.execute(command).fetchall();
c.close()
sys.stdout.write(str(a))
return simplejson.dumps("")
I can't see any differences between the two so I don't know why the GET request is giving me a problem. Any insight into what I might be doing wrong would be helpful.
If you have ideas about the jQuery, CherryPy, config files, anything, I'd really appreciate it.