与逃脱JSON响应[的WebMethod] [英] Escaped JSON response with [WebMethod]

查看:359
本文介绍了与逃脱JSON响应[的WebMethod]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从以下code JSON响应错误地转义为如下所述。

我的webmethod是这样的:

  [的WebMethod(说明=DOC这里)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)
    公共字符串responseMyObject(){
            (!设置())如果返回;            ...
            代理PU =新的代理(...);
...            串toReturn = JavaScriptConvert.SerializeObject(pu.getMyObject());
            Console.WriteLine(toReturn);
            返回toReturn;
    }

从控制台我得到:

  {字段1:vaule1,字段2:VALUE2}

从JS:

  $。阿贾克斯({
    键入:POST,
    网址:/myapi/myClass.asmx/responseMyObject
    数据:{},
    的contentType:应用/ JSON的;字符集= UTF-8,
    数据类型:JSON
    成功:函数(MSG){
                    VAR对象= msg.d;
                    警报(object.field1);
    }
});

问题是,在HTTP响应头,我可以看到JSON响应是错误地躲过以下列方式(?):

  {D:{\\字段1 \\:值1,\\场2 \\:VALUE2}

有什么奇怪的是,在控制台打印是罚款(但尚未封装在{D:...}

  {字段1:值1,字段2:VALUE2}

,与同类code,如果我叫[的WebMethod]返回基本类型(没​​有对象)JSON的反应是好的。这样的:

{D:8080}


解决方案

  [WebService的]
[ScriptService]
公共类为MyWebService:WebService的
{  [的WebMethod(说明=DOC这里)]
  [ScriptMethod(UseHttpGet =假,ResponseFormat = ResponseFormat.Json)
  公共MyObjectType responseMyObject()
  {
      代理PU =新的代理(...);      返回pu.GetMyObject();
  }}

您不需要一个JSON序列,与ScriptService属性标记它给它绑在JSON序列化能力出来。你是pre序列化JSON,然后再其序列:(

The JSON response from the following code is wrongly escaped as described below.

My webmethod is like this:

    [WebMethod (Description="doc here")]
    [ScriptMethod(ResponseFormat=ResponseFormat.Json)] 
    public string responseMyObject() {
            if (!Setup()) return "";

            ...
            Proxy pu = new Proxy(...);
...

            string toReturn = JavaScriptConvert.SerializeObject(pu.getMyObject());
            Console.WriteLine(toReturn);
            return toReturn;
    }

from the console I get:

{"field1":vaule1,"field2":value2}

from JS:

$.ajax({
    type: "POST",
    url: "/myapi/myClass.asmx/responseMyObject",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
                    var object = msg.d;
                    alert(object.field1);
    }
});

The problem is that in the HTTP response header I can see that the JSON response is wrongly (?) escaped in the following way:

{"d":"{\"field1\":value1,\"field2\":value2}"}

What's strange is that the console print is fine (but not yet encapsulated in {d: ...}

{"field1":value1,"field2":value2}

With similar code, if I call a [WebMethod] that returns basic types (no object) the JSON response is ok. Like:

{"d":8080}

解决方案

[WebService]
[ScriptService]
public class MyWebService : WebService
{    

  [WebMethod (Description="doc here")]    
  [ScriptMethod( UseHttpGet=false, ResponseFormat=ResponseFormat.Json)]     
  public MyObjectType responseMyObject() 
  {
      Proxy pu = new Proxy(...);

      return pu.GetMyObject();
  }

}

You dont need a JSON serializer, tagging it with the ScriptService attribute gives it tie ability to serialize JSON out. You were pre serializing the JSON and then serializing it again :(

这篇关于与逃脱JSON响应[的WebMethod]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆