JQuery,使用 GET 方法发送 JSON 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10948233/
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
JQuery, send JSON object using GET method
提问by Roman Semko
I am trying to send a json object using GET method. My code:
我正在尝试使用 GET 方法发送一个 json 对象。我的代码:
$.ajax({
url: "/api/endpoint",
type: "GET",
data: {"sort":"date"},
contentType: "application/json",
dataType: "json",
...
However, the headers received have "Content-Length" set to zero, hence my json parser on the server doesn't read the content.
但是,收到的标头将“Content-Length”设置为零,因此服务器上的 json 解析器不会读取内容。
I have already tried setting content length header, but it still comes to the server as zero:
我已经尝试设置内容长度标头,但它仍然为零:
$.ajax({
url: "/api/endpoint",
headers: {"CONTENT_LENGTH",JSON.stringify({"sort":"date"}).length},
type: "GET",
data: {"sort":"date"},
contentType: "application/json",
dataType: "json",
...
Any idea how to get this working? It HAS to be GET request.
知道如何让这个工作吗?它必须是 GET 请求。
回答by Bergi
GET requests (at least usually) do not have a message body. As mentioned in the docs, jQuery appends data
of GET requests to the url parameters. You should be able to read your sort
parameter from there with your server application.
GET 请求(至少通常)没有消息体。如文档中所述,jQuery 将data
GET 请求附加到 url 参数。您应该能够sort
使用您的服务器应用程序从那里读取您的参数。
BTW, no user agent will allow you to set the Content-Length
header - it will (and must) be done automatically depending on the sent data.
顺便说一句,没有用户代理将允许您设置Content-Length
标头 - 它会(并且必须)根据发送的数据自动完成。
回答by UltraInstinct
There are a few places where you have gone a bit wrong.
有几个地方你做错了。
- It is not
CONTENT_LENGTH
, itsContent-Length
. - Do not set
Content-Length
header, the browser will do it for you. - Get request has content-length = 0.
- 它不是
CONTENT_LENGTH
,它的Content-Length
。 - 不要设置
Content-Length
标题,浏览器会为你做。 - 获取请求的内容长度 = 0。
Something like the below should work for you:
像下面这样的东西应该适合你:
$.ajax({
url: "/api/endpoint?parameters="+encodeURIComponent(JSON.stringify({"sort":"date"})),
type: "GET",
...
});
回答by bahaddin.yasar
I think you should use JSON.stringify for GET parameters in URL like this:
我认为你应该使用 JSON.stringify 作为 URL 中的 GET 参数,如下所示:
$.ajax({
url: "/api/endpoint?parameters="+JSON.stringify({"sort":"date"}),
type: "GET",
contentType: "application/json",
dataType: "json",
...
回答by mhu
As mentioned by Bergi, the data is converted by jQuery.ajax()
to request parameters.
From jQuery 1.7.2:
正如 Bergi 所提到的,数据被转换jQuery.ajax()
为请求参数。从 jQuery 1.7.2 开始:
// Determine if request has content
s.hasContent = !rnoContent.test( s.type ); --> false when s.type == "GET'
...
...
if ( !s.hasContent ) {
// If data is available, append data to url
if ( s.data ) {
s.url += ( rquery.test( s.url ) ? "&" : "?" ) + s.data;
// #9682: remove data so that it's not used in an eventual retry
delete s.data;
}