开发者

Why is my post jQuery Ajax request seinding JSON?

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开发者_StackOverflow, // 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜