asp.net - 有奇怪的 {"d":null} in JSON response

标签 asp.net json

请对 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 字符串响应如下,带有额外的奇怪 {"d":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.
请有人帮我解决这个问题,已经做了好几个星期了

非常感谢

最佳答案

不推荐使用 JavascriptSerializer。它早在大多数事实上的 JSON 标准之前创建,当然在任何实际标准之前。例如,它不使用事实上的 ISO8601 日期格式。

微软 own documentation在最上面说:

Json.NET should be used serialization and deserialization. Provides serialization and deserialization functionality for AJAX-enabled applications.



Microsoft 本身在 ASP.NET MVC Web API 中使用 Json.NET。

Json.NET 您可以使用 JsonConvert.Serialize 从对象列表生成 Json 字符串:
Dim strResponse As String = JsonConvert.Serialize(accs)

关于asp.net - 有奇怪的 {"d":null} in JSON response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44303781/

相关文章:

c# - 如何从 sql 数据库中检索 asp.net mvc 中的行数?

android - map View 使用 google Directions API 绘制方向 - 解码多段线

c# - 使用 .net 标准 2.0 将 .net4.0 旧库转换为多个目标会出现 HtmlString 引用错误

ASP.NET 关闭 FriendlyURLs mobile.master 页面

javascript - 将 Google Sheet 转换为多级 JSON 数组

javascript - KineticJS:保存到 JSON:对象保存多次

java - 如何将 Android 应用程序连接到 API 并在列表中显示数据?

c++ - 准备 C++ Json 请求

c# - 这些对象的实例化方式有何不同?

c# - 外键约束错误但不应该发生