Chrome 中的 JQuery Ajax 发送 GET 而不是 POST,但在 Edge 中,它会正确发送

JQuery Ajax in Chrome sends GET instead of POST, but in Edge it sends correctly

提问人:Nimal 提问时间:2/7/2022 最后编辑:ADysonNimal 更新时间:2/16/2022 访问量:278

问:

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

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

它发生在 Chrome 中。

enter image description here

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

enter image description here

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

jQuery ajax Google-chrome

评论

1赞 aynber 2/7/2022
尝试而不是method: 'POST'type: 'POST'
0赞 Rory McCrossan 2/7/2022
第一个屏幕截图中的querystring参数对逻辑有什么影响?控制台中是否有任何错误?此 AJAX 请求是否在提交表单时发出,并且该表单提交未被阻止?我的观点是,发生这种情况的原因有很多,但是如果没有看到创建问题的代码的更完整示例,我们无法有效地帮助您。任何这样做的尝试都是一种猜测。
0赞 ADyson 2/7/2022
我们需要一个最小的可重现问题示例。jQuery $.ajax 本身不会这样做,或者至少在不同的浏览器 AFAIK 中没有区别。我想知道是否还有其他一些 JS 问题可能会导致直接提交表单而不是一些 JS 触发,或者类似的东西。虽然现在很难看出 Chrome 中会出现什么问题,但 Edge 却没有,但它们非常兼容。
1赞 epascarello 2/7/2022
"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
0赞 Nimal 2/16/2022
问题出在通过 或 处理请求时。我已经发布了一个单独的答案。http://127.0.0.1:8000/http://localhost:8000/

答:

-1赞 Vlad 2/7/2022 #1

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

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

有关更多信息,请参见 jQuery ajax 文档

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

评论

0赞 ADyson 2/7/2022
it would only solve the problem if you are using an old jQuery Version...不,恰恰相反。“type” 和 “method” 从 1.9 开始都有效。但是如果 OP 使用的是 <1.9.0,那么从“类型”更改为“方法”会导致问题,而不是解决问题,因为在那些旧版本中它无法识别“方法”。所以这不是解决方案。
0赞 Nimal 2/16/2022 #2

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

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

所以,现在我正在使用 .http://localhost:8000/