为什么我的帖子 jQuery Ajax 请求是 JSON?
我下载了一些代码,其中有以下片段:
function GetCommentBySessionIDWCF_JSON() {
varType = "POST";
varUrl = "service/CommentSessionIDWCFService.svc/GetCommentsByPost";
varData = '{"SessionID": "' + '123' + '"}';
varContentType = "application/json; charset=utf-8";
varDataType = "json";
varProcessData = true;
//now to do the clever stuff
$.ajax({
type: varType, //GET or POST or PUT or DELETE verb
url: varUrl, // Location of the service
data: varData, //Data sent to server
contentType: varContentType, // content type sent to server
dataType: varDataType, //Expected data format from server
processdata: varProcessData, //True or False
success: function (data) {//On Successfull service call
$.each(data.GetCommentsByPostResult, function (e) {
alert(e.CommentText);
});
},
error: ServiceFailed// When Service call fails
});
我想知道为什么我必须在这篇文章中发送 JSON?我阅读了 jQuery 文档,它说:
“要发送到服务器的数据。它会转换为查询字符串(如果还不是字符串)。它会附加到 GET 请求的 url 中。请参阅 processData 选项以防止这种自动处理。对象必须是键/值对。如果值是数组,jQuery 将根据传统设置的值序列化具有相同键的多个值(如下所述)。”
但是当我更改了 JSON 时“数据”到字符串我收到 400 错误。为什么?
I downloaded some code and in it was the following fragment:
function GetCommentBySessionIDWCF_JSON() {
varType = "POST";
varUrl = "service/CommentSessionIDWCFService.svc/GetCommentsByPost";
varData = '{"SessionID": "' + '123' + '"}';
varContentType = "application/json; charset=utf-8";
varDataType = "json";
varProcessData = true;
//now to do the clever stuff
$.ajax({
type: varType, //GET or POST or PUT or DELETE verb
url: varUrl, // Location of the service
data: varData, //Data sent to server
contentType: varContentType, // content type sent to server
dataType: varDataType, //Expected data format from server
processdata: varProcessData, //True or False
success: function (data) {//On Successfull service call
$.each(data.GetCommentsByPostResult, function (e) {
alert(e.CommentText);
});
},
error: ServiceFailed// When Service call fails
});
What im wondering is why I have to send JSON with this post? I read the jQuery documentation and it says:
"Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below)."
But when I changed the JSON in 'data' to a string I get a 400 error. why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它不是 JSON,而是一个包含键/值对的对象,该对象被呈现为 HTTP
?param=value
以发送到服务器。It's not JSON, it's an object containing key/value pairs that is rendered to HTTP
?param=value
to be sent to the server.