Javascript 使用 jQuery $.ajax() 时如何使用 GET 在请求正文中发送数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10298899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send data in request body with a GET when using jQuery $.ajax()
提问by Oliver Pearmain
The service API I am consuming has a given GET method that requires the data be sent in the body of the request.
我正在使用的服务 API 有一个给定的 GET 方法,该方法要求在请求正文中发送数据。
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.
正文中所需的数据是一个由连字符分隔的 id 列表,可能非常大,因此必须在正文中发送,否则它可能会在浏览器/代理/网络服务器等链中的某个位置出现 foobar。请注意,我无法控制服务或 API,因此请不要提出更改建议。
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...
我正在使用以下 jQuery 代码,但是在 fiddler 中观察请求/响应我可以看到我发送的“数据”总是被转换并附加到查询字符串,尽管我将“processData”选项设置为 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.
任何人都知道如何强制在请求正文中发送“数据”值?任何帮助表示赞赏,提前致谢。
采纳答案by EndangeredMassa
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 specsays 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.
通常,这不是系统使用 GET 请求的方式。因此,很难让您的库发挥作用。事实上,规范说“如果请求方法是 GET 或 HEAD 的区分大小写匹配,就像数据为空一样。” 所以,我认为你运气不好,除非你使用的浏览器不尊重规范的那部分。
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.
您可以在自己的服务器上为 POST ajax 请求设置一个端点,然后在您的服务器代码中将其重定向到带有正文的 GET 请求。
If you aren't absolutely tied to GET requests with the body being the data, you have two options.
如果您不完全依赖于主体为数据的 GET 请求,您有两种选择。
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.
POST 数据:这可能是您想要的。如果您正在传递数据,那可能意味着您正在修改某些模型或在服务器上执行某些操作。这些类型的操作通常通过 POST 请求完成。
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'
回答by Paulo Mota
You can send your data like the "POST" request through the "HEADERS".
您可以通过“HEADERS”发送数据,例如“POST”请求。
Something like this:
像这样的东西:
$.ajax({
url: "htttp://api.com/entity/list($body)",
type: "GET",
headers: ['id1':1, 'id2':2, 'id3':3],
data: "",
contentType: "text/plain",
dataType: "json",
success: onSuccess,
error: onError
});
回答by Neskews
Just in case somebody ist still coming along this question:
以防万一有人还在提出这个问题:
There is a body query object in any request. You do not need to parse it yourself.
任何请求中都有一个正文查询对象。您不需要自己解析它。
E.g. if you want to send an accessToken from a client with GET, you could do it like this:
例如,如果您想使用 GET 从客户端发送 accessToken,您可以这样做:
const request = require('superagent');
request.get(`http://localhost:3000/download?accessToken=${accessToken}`).end((err, res) => {
if (err) throw new Error(err);
console.log(res);
});
The server request object then looks like {request: { ... query: { accessToken: abcfed } ... } }
服务器请求对象然后看起来像 {request: { ... query: { accessToken: abcfed } ... } }