我试图用GET方法发送一个JSON对象。我的code:
$。阿贾克斯({
网址:/ API /终点,
键入:GET,
数据:{之类的:日期},
的contentType:应用/ JSON
数据类型:JSON,
...
不过,收到了标题有内容长度设定为零,因此,我在服务器上的JSON解析器不阅读的内容。
我已经尝试设置内容长度的头,但它仍然涉及到服务器为零:
$。阿贾克斯({
网址:/ API /终点,
标题:{CONTENT_LENGTH,JSON.stringify({排序:日期}),长度},
键入:GET,
数据:{之类的:日期},
的contentType:应用/ JSON
数据类型:JSON,
...
任何想法如何得到这个工作?它必须是GET请求。
解决方案 GET请求(至少通常)没有邮件正文。正如INT 的文档,jQuery的追加数据
的GET请求该URL参数。您应该能够从那里读你的排序
参数与您的服务器应用程序。
顺便说一句,没有用户代理将允许您设置内容长度
头 - 它将(必须)自动根据发送的数据来完成。
I am trying to send a json object using GET method. My code:
$.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.
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 request (at least usually) have not message body. As mentioned int 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.
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.
相关推荐
最新文章