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

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

主体中所需的数据是一个由连字符分隔的 id 列表,可能非常大,因此必须在主体中发送,否则它可能会在浏览器/代理/网络服务器等链中的某个地方出错。注意,我没有对服务或 API 的控制权,所以请不要提出改变它的建议。

我正在使用下面的 jQuery 代码,但是通过观察 fiddler 中的请求/响应,我可以看到我发送的“数据”是 ALWAYS 转换后附加到查询字符串中的,尽管我将“ 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
});

有人知道如何强制在请求的正文中发送“数据”值吗? 如有任何协助,我们将不胜感激。

216756 次浏览

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.

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.

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

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'

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:

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 } ... } }

we all know generally that for sending the data according to the http standards we generally use POST request. But if you really want to use Get for sending the data in your scenario I would suggest you to use the query-string or query-parameters.

1.GET use of Query string as. \{\{url}}admin/recordings/some_id

here the some_id is mendatory parameter to send and can be used and req.params.some_id at server side.

2.GET use of query string as\{\{url}}admin/recordings?durationExact=34&isFavourite=true

here the durationExact ,isFavourite is optional strings to send and can be used and req.query.durationExact and req.query.isFavourite at server side.

3.GET Sending arrays \{\{url}}admin/recordings/sessions/?os["Windows","Linux","Macintosh"]

and you can access those array values at server side like this

let osValues = JSON.parse(req.query.os);
if(osValues.length > 0)
{
for (let i=0; i<osValues.length; i++)
{
console.log(osValues[i])
//do whatever you want to do here
}
}