繁体   English   中英

为什么我的jQuery Ajax帖子请求隐藏JSON?

[英]Why is my post jQuery Ajax request seinding 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选项以防止此自动处理。对象必须是键/值对。如果value是一个Array,则jQuery会根据传统设置的值使用相同的键序列化多个值(如下所述)。”

但是,当我将“数据”中的JSON更改为字符串时,出现400错误。 为什么?

它不是JSON,而是一个包含键/值对的对象,该对象被呈现为HTTP ?param=value并发送到服务器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM