0

我已经完全重写了这个旧的 asmx 服务函数,但我仍然无法让它返回 JSON。即使我使用 ajax() 并将数据类型和内容类型设置为 json,它也会返回 XML。我正在尝试将此功能与 Jquery dataTables 一起使用。而且我知道有很多这样的问题,但我发现的都是 C#,我无法适应它们。

完整 asmx 文件的最新 pastebin :http : //pastebin.com/swXKqgd4

新代码

   <WebMethod()> _
    <WebGet(ResponseFormat:=WebMessageFormat.Json)> _
    Public Function rptPendingServerRequests() As Generic.List(Of request)
        Dim _conn As SqlConnection = New SqlConnection(connectionString)
        Dim _dr As SqlDataReader
        Dim Sql As String = String.Empty
        Sql += "<My query here>"

        Try
            Dim _cmd As SqlCommand = New SqlCommand(Sql, _conn)
            _conn.Open()
            _dr = _cmd.ExecuteReader(CommandBehavior.CloseConnection)

            If _dr.HasRows Then
                Dim s As request
                Dim c As New Generic.List(Of request)
                While _dr.Read
                    s = New request
                    With s
                        .requestID = _dr("request_id")
                        .status = _dr("status")
                        .requester = _dr("req_by_user_id")
                        .assignee = _dr("user_id")
                        .nextAction = _dr("description")
                    End With
                    c.Add(s)
                End While

                Return c
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            _conn.Close()
        End Try

    End Function

新班级

<Serializable()> _
Public Class request
    Private _requestID As Integer
    Public Property requestID() As Integer
        Get
            Return _requestID
        End Get
        Set(ByVal value As Integer)
            _requestID = value
        End Set
    End Property

    Private _requester As String
    Public Property requester() As String
        Get
            Return _requester
        End Get
        Set(ByVal value As String)
            _requester = value
        End Set
    End Property

    Private _requestDate As Date
    Public Property requestDate() As Date
        Get
            Return _requestDate
        End Get
        Set(ByVal value As Date)
            _requestDate = value
        End Set
    End Property

    Private _status As Integer
    Public Property status() As Integer
        Get
            Return _status
        End Get
        Set(ByVal value As Integer)
            _status = value
        End Set
    End Property

    Private _assignee As String
    Public Property assignee() As String
        Get
            Return _assignee
        End Get
        Set(ByVal value As String)
            _assignee = value
        End Set
    End Property

    Private _nextAction As String
    Public Property nextAction() As String
        Get
            Return _nextAction
        End Get
        Set(ByVal value As String)
            _nextAction = value
        End Set
    End Property


End Class

原始代码

<WebMethod()> _
<WebGet(ResponseFormat:=WebMessageFormat.Json)> _
Public Function rptPendingServerRequestsOld() As DataSet
    Dim connection As SqlConnection
    Dim command As SqlCommand
    Dim adapter As New SqlDataAdapter
    Dim ds As New DataSet
    Dim sql As String
    sql = ""
    sql += "<MY query here>"

    connection = New SqlConnection(connectionString)

    Try
        connection.Open()
        command = New SqlCommand(sql, connection)
        adapter.SelectCommand = command
        adapter.Fill(ds)
        adapter.Dispose()
        command.Dispose()
        connection.Close()
        Return ds

    Catch ex As Exception
    End Try
End Function

客户

        $('#report').dataTable({
            "bProcessing": true,
            "sAjaxSource": 'reportdata.asmx/rptPendingServerRequests'
        });
4

2 回答 2

0

更改此行。

Public Function rptPendingServerRequests() As Generic.List(Of request)

Public Function rptPendingServerRequests() As String.
于 2014-12-26T04:53:28.877 回答
0

由于您是从 JS 调用此方法而不是

<WebGet(ResponseFormat:=WebMessageFormat.Json)>

利用

<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> 

属性。也不要忘记用

<ScriptService()> 

属性。

于 2013-09-04T15:08:07.463 回答