WCF将数据转换为原始html而不是Json格式

时间:2014-01-09 18:53:51

标签: json wcf dynatree

对于WCF来说是非常新的,除了从JQuery到WCF的帖子之外几乎一切正常。

Jquery正在处理DynaTree并将数据发布到WCF服务。 到现在为止还挺好。

当我调试时,我发现在WCF例程中作为Stream接收的发布数据是原始格式并包含HTML字符%...

我期待帖子以Json格式发送数据,但不清楚问题出在哪里。 事实上,我不得不在Post动作中注释掉内容类型,因为它会产生一个错误,说明预期会有RAW格式。

WCR URL请求有点像localhost / TS.svc / SaveTree / New?username = thisuser

    [OperationContract]
    [WebInvoke(Method = "POST",
    RequestFormat = WebMessageFormat.Json, 
    ResponseFormat = WebMessageFormat.Json,
    UriTemplate = "SaveTree/New",
    BodyStyle = WebMessageBodyStyle.Wrapped)]

    public List<TreeNode> SaveTree(Stream data)
    {
        string username = HttpContext.Current.Request.QueryString["username"] ?? string.Empty; 

        var jsonString = new StreamReader(data).ReadToEnd(); 

...}

JQuery方面:

onDrop: function(node, sourceNode, hitMode, ui, draggable) {

            sourceNode.move(node, hitMode);

            var dict = $("#tree").dynatree("getTree").toDict();

            var postData = JSON.stringify(dict); 
            $.ajax({

                type: "POST",

                url: "TS.svc/SaveUserTree/New?userame=" + $("#hdnUserFile").val(),

                //contentType: "application/json; charset=utf-8",

                dataType: "json",

                data: {tree:postData},

请帮忙。谢谢,

1 个答案:

答案 0 :(得分:2)

在您移除contentType: "application/json; charset=utf-8"后,您的内容类型现为application/x-www-form-urlencoded; charset=UTF-8

所以,你看到的是URL编码的JSON。要将其解码为实际的JSON,您可以使用:

var decodedJSON = HttpUtility.UrlDecode(jsonString);
相关问题