DELETE request from jQuery to CherryPy not sending parameters
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() {
开发者_JS百科 $.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?
It appears you're trying to send a DELETE request with a request body, which is ... unusual. (The same would apply to GET).
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:
cherrypy.request.body.readline()
it returns
b'somekey=2cefe65093df'
you can do something like that
import urllib.parse
...
urllib.parse.parse_qs(cherrypy.request.body.readline().decode("utf-8"))
it returns
{'somekey': ['2cefe65093df']}
精彩评论