使用 jQuery $.ajax() 时如何使用 GET 在请求正文中发送数据 [英] How to send data in request body with a GET when using jQuery $.ajax()

查看:31
本文介绍了使用 jQuery $.ajax() 时如何使用 GET 在请求正文中发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的服务 API 有一个给定的 GET 方法,该方法要求在请求正文中发送数据.

The service API I am consuming has a given GET method that requires the data be sent in the body of the request.

正文中所需的数据是由连字符分隔的 id 列表,并且可能非常大,因此必须在正文中发送,否则它可能会在浏览器/代理/网络服务器等链中的某个地方使用 foobar.请注意,我无法控制服务或 API,因此请不要提出更改建议.

The data required in the body is a list of id's separated by hypen and could potentially be very large and thus it must be sent in the body otherwise it will likely foobar somewhere in the browsers/proxies/webservers etc chain. Note I don't have control over the service or API so please don't make suggestions to change it.

我正在使用以下 jQuery 代码,但是在 fiddler 中观察请求/响应我可以看到我发送的数据"总是被转换并附加到查询字符串,尽管我将processData"选项设置为 false...

I am using the following jQuery code however observing the request/response in fiddler I can see that the "data" I am sending is ALWAYS converted and appended to the query string despite me setting the "processData" option to false...

$.ajax({
   url: "htttp://api.com/entity/list($body)",
   type: "GET",
   data: "id1-id2-id3",
   contentType: "text/plain",
   dataType: "json",
   processData: false, // avoid the data being parsed to query string params
   success: onSuccess,
   error: onError
});

有谁知道我如何强制在请求正文中发送数据"值?感谢任何帮助,提前致谢.

Anyone know how I can force the "data" value to be sent in the body of the request? Any assistance is appreciated, thanks in advance.

推荐答案

一般来说,这不是系统使用 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'

这篇关于使用 jQuery $.ajax() 时如何使用 GET 在请求正文中发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆