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

发布于 2024-11-08 12:47:13 字数 1408 浏览 0 评论 0原文

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

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狠疯拽 2024-11-15 12:47:13

它不是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文