请按以下方式对Web API进行AJAX调用
$.ajax({
type: "POST",
url: "CustomerService.asmx/GetAccounts",
data: '{"custid":"' + cusID + '"}',
contentType: "application/json; charset-utf-8",
dataType: "json",
success: function (data) {
datatableVariable = $('#dtAccounts').DataTable({.......
这是我的ASP.NET Web方法
<WebMethod()>
Public Sub GetAccounts(ByVal custid As Integer)
Dim sql As String = "Select * from Accounts WHERE CustomerID =" & custid & " Order by ID"
Dim constr As String = ConfigurationManager.ConnectionStrings("flashCon").ConnectionString
Dim accs = New List(Of Accounts)()
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand(sql)
cmd.CommandType = CommandType.Text
cmd.Connection = con
con.Open()
Dim dr = cmd.ExecuteReader()
If dr.HasRows Then
While dr.Read
Dim acc = New Accounts() With {
.custID = Convert.ToInt32(dr("CustomerID").ToString()),
.AccNum = dr("AccountNumber").ToString(),
.Descri = dr("Description").ToString(),
.CurrBalance = dr("CurrentBalance").ToString(),
.typOA = dr("TypeOfAccount").ToString(),
.Frozen = dr("Frozen").ToString()
}
accs.Add(acc)
End While
End If
End Using
End Using
Dim js = New JavaScriptSerializer()
Dim strResponse As String = js.Serialize(accs)
Context.Response.Write(strResponse)
End Sub
返回的JSON字符串响应是下面的另外一个奇怪的{&#34; d&#34;:null}值,我不知道它来自哪里
[{"custID":2,"AccNum":"KODSW00002","Descri":"","CurrBalance":0,"Frozen":false,"typOA":0}]{"d":null}
如果我使用Context.Response.Flush()
它会消失,但我会从浏览器Server cannot clear headers after HTTP headers have been sent.
有人可以协助我解决这个问题,现在已经好几个星期了吗
非常感谢
答案 0 :(得分:2)
不推荐使用JavascriptSerializer。它早在大多数事实上的JSON标准之前创建,当然在任何实际标准之前。例如,它不使用事实上的ISO8601日期格式。
微软的own documentation在最顶层说:
Json.NET应该用于序列化和反序列化。为支持AJAX的应用程序提供序列化和反序列化功能。
Microsoft本身在ASP.NET MVC Web API中使用Json.NET。
使用Json.NET ,您可以使用JsonConvert.Serialize从对象列表生成Json字符串:
Dim strResponse As String = JsonConvert.Serialize(accs)
答案 1 :(得分:0)
尝试一下
$.ajax({
type: "POST",
url: "CustomerService.asmx/GetAccounts",
data: {custid: cusID},
dataType: "json",
success: function (data) {
alert(data.d); // Should correctly get the response in the d variable.
秘密在于JavaScriptSerializer不需要 contentType:“应用程序/ json; charset-utf-8”,
,并且数据中的参数没有撇号或直接用双引号引起来
更多信息,请访问:https://www.youtube.com/watch?v=Zq_aMDFeCCA
致谢
答案 2 :(得分:0)
在使用Context.Response.Clear()
在服务器端提供响应并设置contentType
之前,我们必须从ASP.NET Web服务中清除默认响应 {d:null} 。
Context.Response.Clear()
Context.Response.ContentType = "application/json"
Context.Response.AddHeader("content-length", stringJSON.Length.ToString())
Context.Response.Flush()
Context.Response.Write(stringJSON)
HttpContext.Current.ApplicationInstance.CompleteRequest()
答案 3 :(得分:-1)
你得到{&#34; d&#34;:null},因为你的WebMethod没有返回值,你只是写入Response对象。
您应该从方法中返回一个字符串:
Public Sub GetAccounts(ByVal custid As Integer) As String
Dim sql As String = "Select * from Accounts WHERE CustomerID =" & custid & " Order by ID"
Dim constr As String = ConfigurationManager.ConnectionStrings("flashCon").ConnectionString
Dim accs = New List(Of Accounts)()
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand(sql)
cmd.CommandType = CommandType.Text
cmd.Connection = con
con.Open()
Dim dr = cmd.ExecuteReader()
If dr.HasRows Then
While dr.Read
Dim acc = New Accounts() With {
.custID = Convert.ToInt32(dr("CustomerID").ToString()),
.AccNum = dr("AccountNumber").ToString(),
.Descri = dr("Description").ToString(),
.CurrBalance = dr("CurrentBalance").ToString(),
.typOA = dr("TypeOfAccount").ToString(),
.Frozen = dr("Frozen").ToString()
}
accs.Add(acc)
End While
End If
End Using
End Using
Dim js = New JavaScriptSerializer()
Dim strResponse As String = js.Serialize(accs)
<strike>Context.Response.Write(strResponse)</strike>
替换为:
Return strResponse
End Sub
然后返回的对象将是{&#34; d&#34;:&#34; [{&#34; custID&#34;:2,&#34; AccNum&#34;:&#34; KODSW00002&#34;&#34; Descri&#34;:&#34;&#34;&#34; CurrBalance&#34;:0,&#34;冷冻及#34;:假,&#34; typOA& #34;:0}]&#34;},可以通过javascript中的response.d进行访问。
$.ajax({
type: "POST",
url: "CustomerService.asmx/GetAccounts",
data: '{"custid":"' + cusID + '"}',
contentType: "application/json; charset-utf-8",
dataType: "json",
success: function (data) {
alert(data.d); // Should correctly get the response in the d variable.