是否可以在 JSON 中更改页面 webmethod 响应,删除 .d 节点?

Posted

技术标签:

【中文标题】是否可以在 JSON 中更改页面 webmethod 响应,删除 .d 节点?【英文标题】:Is it possible to change a page webmethod response in JSON, removing the .d node? 【发布时间】:2019-01-15 12:59:41 【问题描述】:

我知道我可以使用 ASHX 处理程序来实现这一点,但我确实需要通过页面 webmethod 来实现。

我正在使用此代码从页面 javascript 中调用页面 webmethod:

        $.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"
    ]
  ]

并尝试了几种 webmethod 的变体...

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 对象,然后将其 serealize 并写入当前上下文

<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对象,然后serealize并返回

<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 实现这一点吗?

【问题讨论】:

看看这里:***.com/questions/7227427/…。这是一种安全强化机制,因此您可能不想更改它。 您可以编写一个包装 javascript 方法来获取正确的 JSON 【参考方案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),并在方法的主体中,首先使用JavaScriptSerializer 或@987654328 将obj 序列化为json @,然后像上述方法一样将其写入响应。

【讨论】:

离题,但您也可以切换到 Web API 并在您的站点中托管 Web API,与 Web 表单并排。 使用您提供的相同文件测试答案,没有任何问题。如果您对答案有任何疑问,请告诉我。【参考方案2】:

强制使用:

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

这解决了我的问题。

【讨论】:

Reza 的回答不是已经提供了这个建议吗(它实际上试图教育研究人员,而不是简单地抛弃一个 sn-p)?

以上是关于是否可以在 JSON 中更改页面 webmethod 响应,删除 .d 节点?的主要内容,如果未能解决你的问题,请参考以下文章

是否可以通过 Ajax 更改页面源内容变量

在TypeScript中保存代码后是否可以运行grunt命令?

根据使用 json 选择的语言更改 html 页面的内容

页面标签Facebook v2.1更改:如何查看粉丝现在是否喜欢该页面?

向下滚动单个页面时是不是可以更改 url

在 vuepress 中更改 404 页面