【问题标题】:DELETE request from jQuery to CherryPy not sending parameters从 jQuery 到 CherryPy 的 DELETE 请求不发送参数
【发布时间】:2022-01-04 09:32:40
【问题描述】:

由于某种原因,当我从 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 网络服务器打印出来的内容:

{'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 请求没有发送正确的参数吗?

【问题讨论】:

    标签: jquery python http cherrypy http-delete


    【解决方案1】:

    您似乎正在尝试发送带有请求正文的 DELETE 请求,该请求正文是...不寻常。 (这同样适用于 GET)。

    【讨论】:

      【解决方案2】:

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

      cherrypy.request.body.readline()
      

      它返回

      b'somekey=2cefe65093df'
      

      你可以这样做

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

      它返回

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-07
        • 2015-07-10
        • 1970-01-01
        • 2011-01-10
        • 2015-08-08
        • 1970-01-01
        相关资源
        最近更新 更多