从 jQuery 到 CherryPy 的 DELETE 请求不发送参数
由于某种原因,当我从 jQuery (1.4.4) 向 CherryPy 服务器 (3.1.2) 发出 DELETE HTTP 请求时,没有发送任何参数。 POST、GET 和 PUT 请求都可以正常发送参数。
这是 CherryPy 服务器代码:
import cherrypy
class DeleteExample(object):
exposed = True
def PUT(self, *args, **kwargs):
print kwargs
def DELETE(self, *args, **kwargs):
print kwargs
global_conf = {'global': {'server.socket_port': 8080},
'/': {'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
'tools.staticdir.root': '/home/kevin/workspace/delete_example',
'tools.staticdir.on': True,
'tools.staticdir.dir': 'src',
'tools.staticdir.index': 'index.html'}
}
cherrypy.quickstart(DeleteExample(), config=global_conf)
这是带有 jQuery 代码的 index.html:
<html>
<head>
<script type="text/javascript" src="jquery-1.4.4.js"></script>
<script>
$(document).ready(function() {
$.ajax({
type: "PUT",
url: "http://localhost:8080",
dataType: "json",
data: {first: 10, second: 200}
});
$.ajax({
type: "DELETE",
url: "http://localhost:8080",
dataType: "json",
data: {first: 10, second: 200}
});
});
</script>
</head>
<body>
</body>
</html>
这是从 CherryPy Web 服务器打印出来的内容:
{'second': '200', 'first': '10'}
127.0.0.1 - - [23/Jan/2011:04:02:48] "PUT / HTTP/1.1" 200 19 "http://localhost:8080/" "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.04 (lucid) Firefox/3.6.13"
{}
127.0.0.1 - - [23/Jan/2011:04:02:51] "DELETE / HTTP/1.1" 200 19 "http://localhost:8080/" "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.04 (lucid) Firefox/3.6.13"
正如您所看到的,使用 .ajax 函数发出的 PUT 和 DELETE 请求除了类型之外完全相同。但是,由于某种原因,PUT 发送所有参数,而 DELETE 不发送任何参数。
有人知道为什么 DELETE 请求没有发送正确的参数吗?
For some reason when I make a DELETE HTTP request from jQuery (1.4.4) to a CherryPy server (3.1.2), no parameters are being sent. POST, GET and PUT requests are sending parameters just fine.
Here's CherryPy server code:
import cherrypy
class DeleteExample(object):
exposed = True
def PUT(self, *args, **kwargs):
print kwargs
def DELETE(self, *args, **kwargs):
print kwargs
global_conf = {'global': {'server.socket_port': 8080},
'/': {'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
'tools.staticdir.root': '/home/kevin/workspace/delete_example',
'tools.staticdir.on': True,
'tools.staticdir.dir': 'src',
'tools.staticdir.index': 'index.html'}
}
cherrypy.quickstart(DeleteExample(), config=global_conf)
and here's index.html with jQuery code:
<html>
<head>
<script type="text/javascript" src="jquery-1.4.4.js"></script>
<script>
$(document).ready(function() {
$.ajax({
type: "PUT",
url: "http://localhost:8080",
dataType: "json",
data: {first: 10, second: 200}
});
$.ajax({
type: "DELETE",
url: "http://localhost:8080",
dataType: "json",
data: {first: 10, second: 200}
});
});
</script>
</head>
<body>
</body>
</html>
This is what's being printed out from CherryPy web server:
{'second': '200', 'first': '10'}
127.0.0.1 - - [23/Jan/2011:04:02:48] "PUT / HTTP/1.1" 200 19 "http://localhost:8080/" "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.04 (lucid) Firefox/3.6.13"
{}
127.0.0.1 - - [23/Jan/2011:04:02:51] "DELETE / HTTP/1.1" 200 19 "http://localhost:8080/" "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.04 (lucid) Firefox/3.6.13"
As you can see PUT and DELETE requests made with the use of .ajax function are exactly the same except for the type. But, for some reason PUT sends all the parameters while DELETE sends no parameters.
Does anybody have any idea why DELETE request is not sending proper parameters?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎正在尝试发送带有请求正文的 DELETE 请求,这是......不寻常。 (这同样适用于 GET)。
It appears you're trying to send a DELETE request with a request body, which is ... unusual. (The same would apply to GET).
我有一个非常类似的问题,我非常有必要在正文中发送请求。变量
kwargs
也是空的,我找到了这个解决方案:它返回
你可以做类似
它返回的事情
I had a very similar problem, it was extremely necessary for me to send a request in the body. The variable
kwargs
was also empty, I found this solution:it returns
you can do something like that
it returns