修复从GET请求到CherryPy的404:缺少参数错误 [英] Fix a 404: missing parameters error from a GET request to CherryPy

查看:213
本文介绍了修复从GET请求到CherryPy的404:缺少参数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CherryPy制作一个网页,用于客户端的服务器端,HTML,CSS和jQuery。我也在使用mySQL数据库。

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.

我有一个工作表格供用户注册网站 - 创建用户名和密码。我使用jQuery向CherryPy发送一个AJAX POST请求,该请求查询数据库以查看该用户名是否存在。如果用户名存在,请提醒用户,如果不存在,请将其添加到数据库并提示成功。

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

成功的jQuery POST。

Successful jQuery POST.

我想要要更改表单,以便不再检查提交时是否存在用户名,而是根据用户名输入中的模糊事件发出GET请求。该函数被调用,它转到CherryPy,但后来我收到一个错误: HTTPError:(404,'缺少参数:用户名')

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

jQuery GET不成功。

Unsuccessful jQuery GET.

CherryPy :

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("")

我看不出两者之间有任何差异所以我不知道为什么GET请求给我一个问题。任何洞察我可能做错的事情都会有所帮助。

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.

如果您对jQuery,CherryPy,配置文件等有任何想法,我真的很感激。

If you have ideas about the jQuery, CherryPy, config files, anything, I'd really appreciate it.

* * 编辑 * ***

在一些朋友的要求下,这里有更多的信息。

如何计算getdata:

var getdata = $(#username)。val ();

服务器查询的URL :(嗯,这就是PuTTy所说的来自/去服务器的地方,我不知道服务器端废话)

GET / checkUsername?noram HTTP / 1.1

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

推荐答案

Norabora,
以下是您可以尝试的几件事...

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

更改您的jQuery get请求以下内容:

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

或尝试:

var un = $("#username").val();

$.get('checkUsername', { username: un } , function(data) {
    alert(data);
});

查看jQuery文档,您需要提供 $。get( )以JSON对象作为参数的函数,而您目前只使用String提供 $。get()方法。

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请求必须采用以下格式:
www.somedomain.com?key=value

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

看起来您的GET请求缺少密钥,但仍包含值:

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

GET /checkUsername?noram HTTP/1.1

我希望以下内容有效:

GET /checkUsername?username=noram HTTP/1.1

这篇关于修复从GET请求到CherryPy的404:缺少参数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆