我知道我可以使用 ASHX 处理程序来实现这一点,但我真的需要从页面网络方法中实现它。
我使用以下代码从页面 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"
]
]
}
并尝试了网络方法的几种变体...
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 节点,直接从响应而不是从响应.d 接收 JSON。有什么方法可以从页面网络方法中实现这一点吗?
我真的需要摆脱
.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
或 Newtonsoft.JsonConvert
将obj
序列化为 json,然后将其写入响应,如上述方法。
Mandarory 用法:
HttpContext.Current.Response.Flush()
HttpContext.Current.Response.End()
这解决了我的问题。