Fix a 404: missing parameters error from a GET request to CherryPy

时光总嘲笑我的痴心妄想 提交于 2019-12-10 15:37:54

问题


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!")
    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.

* *EDIT ****
At the request of some friends, here's some more info.
How I compute the getdata:
var getdata = $("#username").val();

URL being queried by the server: (Well, this is what PuTTy says is coming from/going to the server, I don't know server-side nonsense)
GET /checkUsername?noram HTTP/1.1


回答1:


Norabora, Here are a couple things you can try...

Change your jQuery get request from the following:

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

to:

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);
});

Looking at the jQuery documentation, you need to provide the $.get() function with a JSON object as the parameter, whereas you are currently providing the $.get() method with just a String.


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

It looks like your GET request is missing the key, but still containing the value:

GET /checkUsername?noram HTTP/1.1

I would expect the following to work:

GET /checkUsername?username=noram HTTP/1.1


来源:https://stackoverflow.com/questions/4675723/fix-a-404-missing-parameters-error-from-a-get-request-to-cherrypy

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