【问题标题】:JQuery Ajax in Chrome sends GET instead of POST, but in Edge it sends correctlyChrome 中的 JQuery Ajax 发送 GET 而不是 POST,但在 Edge 中它可以正确发送
【发布时间】:2022-02-16 21:16:17
【问题描述】:

以下代码触发 GET 方法而不是 POST。

$.ajax({type: 'POST',url: 'http://localhost:8000/DoAsync',data: {i:e},dataType: 'script'            
    });

这发生在 Chrome 中。

但是,当我在 Edge 中使用相同的代码时,它会正确触发。

不知道如何在 Chrome 中解决?我试图清除缓存。同样的结果。

【问题讨论】:

  • 试试method: 'POST' 而不是type: 'POST'
  • 第一张截图中的querystring参数对逻辑有什么影响?控制台中是否有任何错误?这个 AJAX 请求是在提交表单并且没有阻止该表单提交时发出的吗?我的观点是,发生这种情况有几个原因,但是如果没有看到更完整的代码示例,我们无法有效地帮助您。任何这样做的尝试都是猜测。
  • 我们需要该问题的minimal reproducible example。 jQuery $.ajax 本身不会这样做,或者至少在不同的浏览器中没有不同,AFAIK。我想知道是否还有其他一些 JS 问题可能导致直接提交表单而不是一些 JS 触发,或类似的东西。虽然现在很难看出 Chrome 会出什么问题,但 Edge 却没有,但它们非常兼容。
  • "script": Evaluates the response as JavaScript and returns it as plain text. Disables caching by appending a query string parameter, _=[TIMESTAMP], to the URL unless the cache option is set to true. Note: This will turn POSTs into GETs for remote-domain requests. Prior to jQuery 3.5.0, unsuccessful HTTP responses with a script Content-Type were still executed. api.jquery.com/jquery.ajax
  • 问题在于通过http://127.0.0.1:8000/http://localhost:8000/ 处理请求。我已经发布了一个单独的答案。

标签: jquery ajax google-chrome


【解决方案1】:

当我使用http://127.0.0.1:8000/ 访问服务器时,我在 Chrome 中收到了错误,但在 Edge 中没有。

http://localhost:8000/ 访问服务器,在 Chrome 和 Edge 浏览器中正确执行 POST 方法。

所以,现在我使用的是http://localhost:8000/

【讨论】:

    【解决方案2】:

    405 Method Not AllowedRequest Method: GET 是 chrome 浏览器使用默认方法 GET 的明显标志。

    您可以尝试method: 'POST' 而不是@aynber 已经提到的type: 'POST',但只有在您使用旧的jQuery 版本时才能解决问题。

    See jQuery ajax documentation for more information

    'type' 是'method' 的别名。如果您使用的是 jQuery 1.9.0 之前的版本。

    【讨论】:

    • it would only solve the problem if you are using an old jQuery Version...不,恰恰相反。 "type" 和 "method" 从 1.9 开始都有效。但是如果 OP 使用的是 导致一个问题,而不是解决它,因为在那些旧版本中它不识别“方法”。所以这不是解决方案。
    猜你喜欢
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    • 2014-03-14
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多