我将以下ajax请求发送到在本地主机上运行的服务器。在发送请求之前,我在Firefox中使用REST CLIENT测试了请求iam获得响应并且状态也是200.但是当我从jquery发出请求时iam收到错误。我厌倦了使用firebug iam得到错误的请求来弄清楚错误。我错过了什么吗?
$.ajax({
type: "POST",
url: "http://10.8.2.18:8080/myservice/services/listObject",
data: { "userAgent":null,
"name":"xxx",
"delimiter":"/",
"marker":null,
"prefix":"localmedia/my_datas/"
},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert('Success');
var json = $.parseJSON(data);
alert(json);
},
error: function (data, status, error) {
console.log(data);
console.log(status);
console.log(error);
alert("error");
}
});
});
答案 0 :(得分:0)
尝试:
var request={ "userAgent":null,
"name":"xxx",
"delimiter":"/",
"marker":null,
"prefix":"localmedia/my_datas/"
};
$.ajax({
type: "POST",
url: "http://10.8.2.18:8080/myservice/services/listObject",
data: JSON.stringify(request),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert('Success');
var json = $.parseJSON(data);
alert(json);
},
error: function (data, status, error) {
console.log(data);
console.log(status);
console.log(error);
alert("error");
}
});
});
答案 1 :(得分:0)
您正在传递jQuery一个对象以转换为URL表单编码数据:
data: { "userAgent":null,
"name":"xxx",
"delimiter":"/",
"marker":null,
"prefix":"localmedia/my_datas/"
},
但是你说你要发送JSON:
contentType: "application/json; charset=utf-8",
如果要发送表单编码数据,请删除contentType覆盖。
如果要发送JSON,请使用JSON.stringify
data: JSON.stringify({ "userAgent":null,
"name":"xxx",
"delimiter":"/",
"marker":null,
"prefix":"localmedia/my_datas/"
}),