且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 jQuery $.ajax() 时如何使用 GET 在请求正文中发送数据

更新时间:2021-08-28 22:56:29

一般来说,这不是系统使用 GET 请求的方式.因此,很难让您的库发挥作用.事实上,规范 说如果请求方法是GET 或 HEAD 的区分大小写匹配就像数据为空一样."因此,我认为除非您使用的浏览器不遵守规范的那部分内容,否则您很不走运.

In general, that's not how systems use GET requests. So, it will be hard to get your libraries to play along. In fact, the spec says that "If the request method is a case-sensitive match for GET or HEAD act as if data is null." So, I think you are out of luck unless the browser you are using doesn't respect that part of the spec.

您可以在自己的服务器上为 POST ajax 请求设置一个端点,然后在您的服务器代码中将其重定向到带有正文的 GET 请求.

You can probably setup an endpoint on your own server for a POST ajax request, then redirect that in your server code to a GET request with a body.

如果您不完全依赖于主体为数据的 GET 请求,您有两种选择.

If you aren't absolutely tied to GET requests with the body being the data, you have two options.

POST 数据:这可能就是您想要的.如果您正在传递数据,那可能意味着您正在修改某些模型或在服务器上执行某些操作.这些类型的操作通常通过 POST 请求完成.

POST with data: This is probably what you want. If you are passing data along, that probably means you are modifying some model or performing some action on the server. These types of actions are typically done with POST requests.

使用查询字符串数据获取:您可以将数据转换为查询字符串参数,并以这种方式将它们传递给服务器.

GET with query string data: You can convert your data to query string parameters and pass them along to the server that way.

url: 'somesite.com/models/thing?ids=1,2,3'