是否可以在JSON中更改页面网络方法响应,从而删除.d节点?

时间:2018-08-08 09:52:19

标签: jquery asp.net json vb.net web-services

我知道我可以使用ASHX处理程序来实现此目的,但是我真的需要页面Web方法中的实现。

我正在使用以下代码从页面javascript中调用页面网络方法:

        $.ajax({ type: 'POST', url: '<%= ResolveUrl("~/teste-datatables.aspx/getdata") %>',data: '{ a: ' + id + '}', contentType: 'application/json; charset=utf-8', dataType: 'json',
                success: function (response) {
                        console.log( response);
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        console.log('textStatus:' + textStatus);
                        console.log('errorThrown:' + errorThrown);
                        console.log(XMLHttpRequest);
                    }
            });

我有一个JSON文件teste.json(只是为了保持数据库交互并促进您的测试):

{
  "draw": 1,
  "recordsTotal": 57,
  "recordsFiltered": 57,
  "data": [
    [
      "Airi",
      "Satou",
      "Accountant",
      "Tokyo",
      "28th Nov 08",
      "$162,700"
    ],
    [
      "Angelica",
      "Ramos",
      "Chief Executive Officer (CEO)",
      "London",
      "9th Oct 09",
      "$1,200,000"
    ],
    [
      "Ashton",
      "Cox",
      "Junior Technical Author",
      "San Francisco",
      "12th Jan 09",
      "$86,000"
    ],
    [
      "Bradley",
      "Greer",
      "Software Engineer",
      "London",
      "13th Oct 12",
      "$132,000"
    ],
    [
      "Brenden",
      "Wagner",
      "Software Engineer",
      "San Francisco",
      "7th Jun 11",
      "$206,850"
    ],
    [
      "Brielle",
      "Williamson",
      "Integration Specialist",
      "New York",
      "2nd Dec 12",
      "$372,000"
    ],
    [
      "Bruno",
      "Nash",
      "Software Engineer",
      "London",
      "3rd May 11",
      "$163,500"
    ],
    [
      "Caesar",
      "Vance",
      "Pre-Sales Support",
      "New York",
      "12th Dec 11",
      "$106,450"
    ],
    [
      "Cara",
      "Stevens",
      "Sales Assistant",
      "New York",
      "6th Dec 11",
      "$145,600"
    ],
    [
      "Cedric",
      "Kelly",
      "Senior Javascript Developer",
      "Edinburgh",
      "29th Mar 12",
      "$433,060"
    ]
  ]
}

并尝试了多种web方法...

Test1 将JSON从文件加载到字符串,然后返回

<System.Web.Script.Services.ScriptMethod(ResponseFormat:=ResponseFormat.Json), System.Web.Services.WebMethod(True)>
Public Shared Function getdata(a As Integer) As String
    Dim path As String = HttpContext.Current.Server.MapPath("/teste.json")
    Dim jsonString As String = File.ReadAllText(path)
    Return jsonString.Replace(vbCr, "").Replace(vbLf, "")
End Function

结果是:

{d: "{  "draw": 1,  "recordsTotal": 57,  "recordsFilter ... gh",      "29th Mar 12",      "$433,060"    ]  ]}"}

Test2 将JSON从文件加载到字符串,将其转换为datablesnet对象,然后实现它并写入当前上下文

<System.Web.Script.Services.ScriptMethod(ResponseFormat:=ResponseFormat.Json), System.Web.Services.WebMethod(True)>
Public Shared Sub getdata6(a As Integer)
    Dim path As String = HttpContext.Current.Server.MapPath("/teste.json")
    Dim jsonString As String = File.ReadAllText(path)
    Dim mytable As datatablesnet = Newtonsoft.Json.JsonConvert.DeserializeObject(Of datatablesnet)(jsonString)
    Dim serializer As New JavaScriptSerializer
    HttpContext.Current.Response.ContentType = "application/json"
    HttpContext.Current.Response.Write(serializer.Serialize(mytable))
End Sub

我收到一个错误,并对响应进行了分析,我发现它是(最后添加了 {“ d”:null}

{"draw":1,"recordsTotal":57,"recordsFiltered":57,"data":[["Airi","Satou","Accountant","Tokyo","28th Nov 08","$162,700"],["Angelica","Ramos","Chief Executive Officer (CEO)","London","9th Oct 09","$1,200,000"],["Ashton","Cox","Junior Technical Author","San Francisco","12th Jan 09","$86,000"],["Bradley","Greer","Software Engineer","London","13th Oct 12","$132,000"],["Brenden","Wagner","Software Engineer","San Francisco","7th Jun 11","$206,850"],["Brielle","Williamson","Integration Specialist","New York","2nd Dec 12","$372,000"],["Bruno","Nash","Software Engineer","London","3rd May 11","$163,500"],["Caesar","Vance","Pre-Sales Support","New York","12th Dec 11","$106,450"],["Cara","Stevens","Sales Assistant","New York","6th Dec 11","$145,600"],["Cedric","Kelly","Senior Javascript Developer","Edinburgh","29th Mar 12","$433,060"]]}{"d":null}

Test3 将JSON从文件加载到字符串,将其转换为datablesnet对象,然后重新实现并返回

<System.Web.Script.Services.ScriptMethod(ResponseFormat:=ResponseFormat.Json), System.Web.Services.WebMethod(True)>
Public Shared Function getdata5(a As Integer) As String
    Dim path As String = HttpContext.Current.Server.MapPath("/teste.json")
    Dim jsonString As String = File.ReadAllText(path)
    Dim mytable As datatablesnet = Newtonsoft.Json.JsonConvert.DeserializeObject(Of datatablesnet)(jsonString)
    Dim serializer As New JavaScriptSerializer

    Return serializer.Serialize(mytable)
End Function

结果与 Test1 相同:

{d: "{  "draw": 1,  "recordsTotal": 57,  "recordsFilter ... gh",      "29th Mar 12",      "$433,060"    ]  ]}"}

我真的需要摆脱 .d 节点,直接从 response 而不是从 response.d 接收JSON。有什么办法可以通过网页webmethod实现此目的吗?

2 个答案:

答案 0 :(得分:1)

  

我真的需要摆脱.d节点,直接接收JSON   来自响应,而不来自response.d。有什么办法可以实现   WebMethod页中的这个?

是的,有。您可以将结果直接写到响应中:

<System.Web.Services.WebMethod(True)>
Public Shared Sub getdata(a As Integer)
    Dim path As String = HttpContext.Current.Server.MapPath("/teste.json")
    Dim jsonString As String = System.IO.File.ReadAllText(path)
    HttpContext.Current.Response.Clear()
    HttpContext.Current.Response.ContentType = "application/json; charset=utf-8"
    HttpContext.Current.Response.Write(jsonString)
    HttpContext.Current.Response.Flush()
    HttpContext.Current.Response.End()
End Sub

如果您需要在代码中的多个类中执行此操作,则可以轻松创建共享的Sub WriteJsonToResponse(obj as Object),然后在方法的主体中,首先使用{{将obj序列化为json 1}}或JavaScriptSerializer,然后像上述方法一样将其写入响应中。

答案 1 :(得分:0)

标准用法:

HttpContext.Current.Response.Flush()
HttpContext.Current.Response.End()

那解决了我的问题。