Fix error 404: missing parameters from GET request on CherryPy

I am making a webpage using CherryPy for the server side, HTML, CSS and jQuery on the client side. I also use the mySQL database.

I have a working form for users to register on the site - create a username and password. I am using jQuery to send an AJAX POST request to CherryPy, which queries the database to find out if this username exists. If the username exists, notify the user if it is not, add it to the database and receive a warning.

$.post('submit', postdata, function(data) { alert(data); }); 

Successful jQuery POST.

I want to change the form so that instead of verifying that the username exists when sending, the GET request is executed in the same way as with the blur event from entering the username. The function is called and it goes to CherryPy, but then I get the error message: HTTPError: (404, 'Missing parameters: username') .

 $.get('checkUsername', getdata, function(data) { alert(data); }); 

Bad 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!") 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 don't see the differences between the two, so I don’t know why the GET request gives me a problem. Any understanding of what I might be doing wrong would be helpful.

If you have any ideas about jQuery, CherryPy, configuration files, whatever, I would really appreciate it.

* * EDIT ****
At the request of some friends, here is more information.
How can I calculate getdata:
var getdata = $("#username").val();

URL requested by the server: (Well, this is what PuTTy says, going from server to server, I don’t know the server side stupidities)
GET /checkUsername?noram HTTP/1.1

+4
source share
1 answer

Norabora; Here are a few things you can try ...

Modify your jQuery query as follows:

 $.get('checkUsername', getdata, function(data) { alert(data); }); 

in

 var getdata = $("#username").val(); $.get('checkUsername?username=' + getdata, function(data) { alert(data); }); 

OR try:

 var un = $("#username").val(); $.get('checkUsername', { username: un } , function(data) { alert(data); }); 

$.get() looking at the jQuery documentation, you should provide the $.get() function with a JSON object as a parameter, whereas currently you are only providing the $.get() method as a string.


GET requests should be of the form: www.somedomain.com?key=value

It looks like your GET request is missing a key, but still contains a value:

 GET /checkUsername?noram HTTP/1.1 

I expect the following to work:

 GET /checkUsername?username=noram HTTP/1.1 
+4
source

Source: https://habr.com/ru/post/1335340/


All Articles