gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 22:33:57 24 4
gpt4 key购买 nike

我知道我可以使用 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 对象,然后将其序列化并写入当前上下文

<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 对象,然后将其序列化并返回<​​/p>

<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

结果与测试1相同:

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

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

最佳答案

I really need to get rid of the .d node, receiving the JSON directly from response and not from response.d. Is there any way I can achieve this from page WebMethod?

是的,有。您可以将结果直接写入response:

<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使用 JavaScriptSerializer 到 json或Newtonsoft.JsonConvert ,然后像上面的方法一样将其写入响应中。

关于jquery - 是否可以更改 JSON 中的页面 webmethod 响应,删除 .d 节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51743681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com