我试图使用GET方法发送一个json对象.我的代码:
$.ajax({
url: "/api/endpoint",
type: "GET",
data: {"sort":"date"},
contentType: "application/json",
dataType: "json",
...
Run Code Online (Sandbox Code Playgroud)
但是,收到的标题将"Content-Length"设置为零,因此服务器上的json解析器不会读取内容.
我已经尝试过设置内容长度标题,但它仍然以0为单位来到服务器:
$.ajax({
url: "/api/endpoint",
headers: {"CONTENT_LENGTH",JSON.stringify({"sort":"date"}).length},
type: "GET",
data: {"sort":"date"},
contentType: "application/json",
dataType: "json",
...
Run Code Online (Sandbox Code Playgroud)
知道如何让这个工作吗?它必须得到GET请求.
Ult*_*nct 22
有几个地方你有点不对劲.
CONTENT_LENGTH,它的Content-Length.Content-Length标题,浏览器会为你做.像下面这样的东西应该适合你:
$.ajax({
url: "/api/endpoint?parameters="+encodeURIComponent(JSON.stringify({"sort":"date"})),
type: "GET",
...
});
Run Code Online (Sandbox Code Playgroud)
小智 5
我认为你应该在URL中使用JSON.stringify作为GET参数,如下所示:
$.ajax({
url: "/api/endpoint?parameters="+JSON.stringify({"sort":"date"}),
type: "GET",
contentType: "application/json",
dataType: "json",
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
78445 次 |
| 最近记录: |