jquery - 是否可以在 JSON 中更改页面 webmethod 响应,删除 .d 节点?
问题描述
我知道我可以使用 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 实现这一点吗?
解决方案
我真的需要摆脱
.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
如果您需要在代码中的多个类中执行此操作,您可以轻松地在方法的主体中创建一个共享的和,首先使用or将其Sub WriteJsonToResponse(obj as Object)
序列化为obj
json ,然后将其写入上述方法的响应中。JavaScriptSerializer
Newtonsoft.JsonConvert
推荐阅读
- jquery - 通过单击外部文件内部的链接将外部文件加载到索引模板中
- python-3.x - 我应该将什么 IP 地址传递给 python 服务器程序以使其在 AWS EC2 上运行?
- swift - UICollectionView 中 cellForItemAt 的滚动单元格问题
- image-processing - 使用 ImageMagick 进行 OCR 图像预处理
- asp.net-core - 记录 .net core REST api 返回的所有数据
- sql - 在带有 BETWEEN 的 WHERE 子句中使用 CASE
- reactjs - 如何在外部样式中使用material-ui断点规则?
- php - Laravel 搜索查询无法使用 elquent
- javascript - 为什么 parentNode 替换 td 的所有元素而不是指定的元素?
- javascript - React Native,TypeScript - 无论设置什么配置,类型“any []”上都不存在属性“包含”,“发现”