为什么我的帖子jQuery Ajax请求是SEINDING JSON

Why is my post jQuery Ajax request seinding JSON?

本文关键字:请求 SEINDING JSON Ajax jQuery 我的 为什么      更新时间:2023-09-26

我下载了一些代码,其中有以下片段:

 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 中。请参阅进程数据选项以防止此自动处理。对象必须是键/值对。如果 value 是一个数组,jQuery 会根据传统设置的值(如下所述)使用相同的键序列化多个值。

但是当我将"data"中的 JSON 更改为字符串时,我收到 400 错误。 为什么?

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