JQuery的,用GET方法发送的JSON对象 [英] JQuery, send JSON object using GET method

查看:212
本文介绍了JQuery的,用GET方法发送的JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用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.

这篇关于JQuery的,用GET方法发送的JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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