Cherrypy - 正在接收 JSON 数据 404,'Missing parameters'
Cherrypy - reciving JSON data 404, 'Missing parameters'
我正在使用 Cherrypy Postgresql 从头开始制作自己的网站。我是 beginner,Python 和 Javascript,但我非常热衷于学习这两种编程语言。
我现在想要实现的是 fill up HTML form
-> Send data using
JSON by AJAX
-> Define Rights for a visitor
-> 和 redirect to index
全部完成后。
我与这个错误斗争了好几天,因为我在互联网上找不到如何通过 AJAX 使用 JSON
在 python 中发送数据的示例
正如我所说,我是初学者,所以请指出我编写的代码中的所有不良行为:)
Traceback (most recent call last):
File "C:\Program Files (x86)\Python36-32\lib\site-packages\cherrypy\_cpdispatch.py", line 60, in __call__
return self.callable(*self.args, **self.kwargs)
TypeError: login() missing 1 required positional argument: 'dataString'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files (x86)\Python36-32\lib\site-packages\cherrypy\_cprequest.py", line 670, in respond
response.body = self.handler()
File "C:\Program Files (x86)\Python36-32\lib\site-packages\cherrypy\lib\encoding.py", line 221, in __call__
self.body = self.oldhandler(*args, **kwargs)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\cherrypy\_cpdispatch.py", line 66, in __call__
raise sys.exc_info()[1]
File "C:\Program Files (x86)\Python36-32\lib\site-packages\cherrypy\_cpdispatch.py", line 64, in __call__
test_callable_spec(self.callable, self.args, self.kwargs)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\cherrypy\_cpdispatch.py", line 163, in test_callable_spec
raise cherrypy.HTTPError(404, message=message)
cherrypy._cperror.HTTPError: (404, 'Missing parameters: dataString')
Cherrypy 引擎打印输出
[12/Jun/2017:13:44:03] Testing database is disabled, deleting test users... skipping...
[12/Jun/2017:13:44:03] ====== INIT SQL ======
[12/Jun/2017:13:44:03] Connecting to PostgreSQL database...
[12/Jun/2017:13:44:03] Testing engine is enabled, adding record...
[12/Jun/2017:13:44:03] ====== INIT SRV ======
[12/Jun/2017:13:44:03] ENGINE Bus STARTING
CherryPy Checker:
The Application mounted at '' has an empty config.
[12/Jun/2017:13:44:03] ENGINE Started monitor thread 'Autoreloader'.
[12/Jun/2017:13:44:03] ENGINE Started monitor thread '_TimeoutMonitor'.
C:\Program Files (x86)\Python36-32\lib\site-packages\cherrypy\process\servers.py:411: UserWarning: Unable to verify that the server is bound on 8080
warnings.warn(msg)
[12/Jun/2017:13:44:08] ENGINE Serving on http://0.0.0.0:8080
[12/Jun/2017:13:44:08] ENGINE Bus STARTED
Python登录方式
class Root(object):
exposed = True
@cherrypy.expose
# @cherrypy.tools.json_out()
@cherrypy.tools.json_in()
@cherrypy.tools.accept(media='application/json')
def login(self, dataString):
# cherrypy.request.method == "POST":
self.addHeader()
db = config()
conn = psycopg2.connect(**db)
cur = conn.cursor()
is_logged_in = None
bad_username = None
bad_password = None
json.load(dataString)
# input_json = cherrypy.request.json()
# dataString = input_json["dataString"]
# - Login
username_input = dataString[0]
if username_input == cur.execute("SELECT username FROM users WHERE username = %s;", (username_input)):
username_password = dataString[1]
if username_password == cur.execute("SELECT password FROM users WHERE password = %s;", (username_password)):
return is_logged_in
else:
return bad_password
else:
return bad_username
@cherrypy.expose
def index(self):
self.addHeader()
return "ok"
Javascript .js 覆盖提交功能
$("#myForm").submit(function(){
var dataString = [];
dataString.login = $("#login").val();
dataString.password = $("#password").val();
$.ajax({
type: 'POST',
url: 'http://0.0.0.0:8080/login',
data: JSON.stringify(dataString),
contentType: 'application/json',
dataType: 'json',
success: function(response) {
console.log(response);
},
error: function(response) {
console.log(response);
}
});
});
HTML 文件
<!DOCTYPE html>
<html>
<head>
<link href="style.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script src="example.js"></script>
</head>
<body>
<div class="login-page">
<div class="form">
<form action="/process_form/" method="post">
<!-- id="myForm" class="login-form"> -->
<input type="text" name="login" id="login" placeholder="login"/>
<input type="password" name="password" id="password" placeholder="password"/>
<button form="myForm" type="submit">Login</button>
<p class="message">Not registered? <a href="#">Create an account</a></p>
</form>
</div>
</div>
</body>
</html>
您将在 login
函数中收到的 AJAX 数据不包含在参数 dataString
中(实际上,您不会获得任何参数),而是在 cherrypy.request.json
.
因此您应该按如下方式更改您的登录功能:
@cherrypy.expose
@cherrypy.tools.json_in()
def login(self):
json_obj = cherrypy.request.json
...
请注意,您将收到基于 AJAX 参数的实际对象(字典/列表),而不是字符串。
另一个注意事项:cherrypy.request.json
只有在您设置装饰器 @cherrypy.tools.json_in()
后才会填充(就像您已经设置的那样)。
我正在使用 Cherrypy Postgresql 从头开始制作自己的网站。我是 beginner,Python 和 Javascript,但我非常热衷于学习这两种编程语言。
我现在想要实现的是 fill up HTML form
-> Send data using
JSON by AJAX
-> Define Rights for a visitor
-> 和 redirect to index
全部完成后。
我与这个错误斗争了好几天,因为我在互联网上找不到如何通过 AJAX 使用 JSON
在 python 中发送数据的示例正如我所说,我是初学者,所以请指出我编写的代码中的所有不良行为:)
Traceback (most recent call last):
File "C:\Program Files (x86)\Python36-32\lib\site-packages\cherrypy\_cpdispatch.py", line 60, in __call__
return self.callable(*self.args, **self.kwargs)
TypeError: login() missing 1 required positional argument: 'dataString'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files (x86)\Python36-32\lib\site-packages\cherrypy\_cprequest.py", line 670, in respond
response.body = self.handler()
File "C:\Program Files (x86)\Python36-32\lib\site-packages\cherrypy\lib\encoding.py", line 221, in __call__
self.body = self.oldhandler(*args, **kwargs)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\cherrypy\_cpdispatch.py", line 66, in __call__
raise sys.exc_info()[1]
File "C:\Program Files (x86)\Python36-32\lib\site-packages\cherrypy\_cpdispatch.py", line 64, in __call__
test_callable_spec(self.callable, self.args, self.kwargs)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\cherrypy\_cpdispatch.py", line 163, in test_callable_spec
raise cherrypy.HTTPError(404, message=message)
cherrypy._cperror.HTTPError: (404, 'Missing parameters: dataString')
Cherrypy 引擎打印输出
[12/Jun/2017:13:44:03] Testing database is disabled, deleting test users... skipping...
[12/Jun/2017:13:44:03] ====== INIT SQL ======
[12/Jun/2017:13:44:03] Connecting to PostgreSQL database...
[12/Jun/2017:13:44:03] Testing engine is enabled, adding record...
[12/Jun/2017:13:44:03] ====== INIT SRV ======
[12/Jun/2017:13:44:03] ENGINE Bus STARTING
CherryPy Checker:
The Application mounted at '' has an empty config.
[12/Jun/2017:13:44:03] ENGINE Started monitor thread 'Autoreloader'.
[12/Jun/2017:13:44:03] ENGINE Started monitor thread '_TimeoutMonitor'.
C:\Program Files (x86)\Python36-32\lib\site-packages\cherrypy\process\servers.py:411: UserWarning: Unable to verify that the server is bound on 8080
warnings.warn(msg)
[12/Jun/2017:13:44:08] ENGINE Serving on http://0.0.0.0:8080
[12/Jun/2017:13:44:08] ENGINE Bus STARTED
Python登录方式
class Root(object):
exposed = True
@cherrypy.expose
# @cherrypy.tools.json_out()
@cherrypy.tools.json_in()
@cherrypy.tools.accept(media='application/json')
def login(self, dataString):
# cherrypy.request.method == "POST":
self.addHeader()
db = config()
conn = psycopg2.connect(**db)
cur = conn.cursor()
is_logged_in = None
bad_username = None
bad_password = None
json.load(dataString)
# input_json = cherrypy.request.json()
# dataString = input_json["dataString"]
# - Login
username_input = dataString[0]
if username_input == cur.execute("SELECT username FROM users WHERE username = %s;", (username_input)):
username_password = dataString[1]
if username_password == cur.execute("SELECT password FROM users WHERE password = %s;", (username_password)):
return is_logged_in
else:
return bad_password
else:
return bad_username
@cherrypy.expose
def index(self):
self.addHeader()
return "ok"
Javascript .js 覆盖提交功能
$("#myForm").submit(function(){
var dataString = [];
dataString.login = $("#login").val();
dataString.password = $("#password").val();
$.ajax({
type: 'POST',
url: 'http://0.0.0.0:8080/login',
data: JSON.stringify(dataString),
contentType: 'application/json',
dataType: 'json',
success: function(response) {
console.log(response);
},
error: function(response) {
console.log(response);
}
});
});
HTML 文件
<!DOCTYPE html>
<html>
<head>
<link href="style.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script src="example.js"></script>
</head>
<body>
<div class="login-page">
<div class="form">
<form action="/process_form/" method="post">
<!-- id="myForm" class="login-form"> -->
<input type="text" name="login" id="login" placeholder="login"/>
<input type="password" name="password" id="password" placeholder="password"/>
<button form="myForm" type="submit">Login</button>
<p class="message">Not registered? <a href="#">Create an account</a></p>
</form>
</div>
</div>
</body>
</html>
您将在 login
函数中收到的 AJAX 数据不包含在参数 dataString
中(实际上,您不会获得任何参数),而是在 cherrypy.request.json
.
因此您应该按如下方式更改您的登录功能:
@cherrypy.expose
@cherrypy.tools.json_in()
def login(self):
json_obj = cherrypy.request.json
...
请注意,您将收到基于 AJAX 参数的实际对象(字典/列表),而不是字符串。
另一个注意事项:cherrypy.request.json
只有在您设置装饰器 @cherrypy.tools.json_in()
后才会填充(就像您已经设置的那样)。