从 jQuery 到 CherryPy 的 DELETE 请求不发送参数

发布于 2024-10-13 11:09:28 字数 2097 浏览 8 评论 0原文

由于某种原因,当我从 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)

这是带有 jQ​​uery 代码的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空城缀染半城烟沙 2024-10-20 11:09:28

您似乎正在尝试发送带有请求正文的 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).

⊕婉儿 2024-10-20 11:09:28

我有一个非常类似的问题,我非常有必要在正文中发送请求。变量 kwargs 也是空的,我找到了这个解决方案:

cherrypy.request.body.readline()

它返回

b'somekey=2cefe65093df'

你可以做类似

import urllib.parse
...
urllib.parse.parse_qs(cherrypy.request.body.readline().decode("utf-8"))

它返回的事情

{'somekey': ['2cefe65093df']}

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']}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文