I am trying to send parameters via the internal Datatables Ajax call to a VB.Net WebMethod

I am trying to send parameters via the internal Datatables Ajax call to a VB.Net WebMethod

SportsavvyDevSportsavvyDev Posts: 3Questions: 1Answers: 0

So at this point I have searched the web to my best extent to find a solution to this problem but can't seem to find any that replicates it.

I have not been able to get a parameter to pass to a VB.Net WebMethod via the internal ajax call inside the DataTable() function. I originally was getting the Invalid JSON primitive: firstName error, however I learned that just required a JSON.stringify call to remedy. However when I did that stringify call, instead of it working it threw the same error with this now in place of the original error, "Invalid JSON primitive: %7B&1=%22&2=f&3=i&4=r&5=s&6=t&7=N&8=a&9=m&10=e&11=%22&12=%3A&13=%22&14=d&15=a&16=n&17=%22&18=%7D&reverse=&strReverse=.". At this point I am at a loss at what is happening, I will post my code below so maybe someone else might catch something I am missing.

AJAX:

    $(document).ready(function () {

        var params = { "firstName": "dan" };

        $("#invoicesTable").DataTable(
            {
                processing: true,
                ajax: {
                    type: "POST",
                    url: "Reports/DemoReport.aspx/GetReport",
                    data: JSON.stringify(params),
                    contentType: "Application/json; charset=utf-8",
                    responseType: "json",

                    //manipulate the returned data from the ajax call
                    dataSrc: function (json) {
                        //access d value from JSON since that contains the array of data
                        return json.d;
                    }
                },
                "language": {
                    processing: '<i class="fa fa-spinner fa-spin fa-3x fa-fw"></i><span style="color:red;">Loading...</span> '
                },
                //columns are always lowercase first letters
                columns: [
                    { "data": "checkboxes" },
                    { "data": "registrationKey" },
                    { "data": "registrationDate" },
                    { "data": "amountPaid" },
                ],

                dom: 'Bfrtip', //Required for setup

                //defines all of the header buttons
                buttons: [
                    //page length for render
                    'pageLength',
                    //excel export (customized for visable column export)
                    {
                        extend: 'excelHtml5',
                        exportOptions: {
                            columns: ':visible'
                        }
                    },
                    //csv export (customized for visable column export)
                    {
                        extend: 'csvHtml5',
                        exportOptions: {
                            columns: ':visible'
                        }
                    },
                    //pdf export (customized for visable column export)
                    {
                        extend: 'pdfHtml5',
                        exportOptions: {
                            columns: ':visible'
                        }
                    },
                    //column visabilty
                    'colvis'
                ],
                //add a checkbox to the first column
                columnDefs: [{
                   orderable: false,
                    className: 'select-checkbox',
                    targets: 0
                }
                ],
                //selection option general settings
                select: {
                    style: 'multi'
                },
                order: [[1, 'desc']] //have an order set at the start
            }
        )

    });

VB.NET:

<ScriptService()> _
Partial Class Reports_DemoReport
    Inherits System.Web.UI.Page


'Make a web method of this function so Ajax calls can reach it
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
<WebMethod()> _ 
Public Shared Function GetReport(ByVal firstName As String) As List(Of RegistrationReportLine)
    Try

             Dim constr As String = ConfigurationManager.ConnectionStrings("soaDB").ConnectionString
            Using con As New SqlConnection(constr)
                Using cmd As New SqlCommand("SELECT RegistrationKey, RegistrationDate, AmountPaid " & _
                                " FROM Registration")
                    cmd.Connection = con
                    Dim registrations As New List(Of RegistrationReportLine)()
                    con.Open()
                    Using sdr As SqlDataReader = cmd.ExecuteReader()
                        While sdr.Read()
                            registrations.Add(New RegistrationReportLine() With { _
                             .amountPaid = sdr("AmountPaid").ToString(), _
                             .registrationDate = sdr("RegistrationDate").ToString(), _
                             .registrationKey = sdr("RegistrationKey").ToString() _
                            })
                        End While
                    End Using
                    con.Close()
                    Return registrations
                End Using
            End Using

    Catch ex As Exception
        LogError(ex.Message)
    End Try
End Function
End Class

Model(Also VB.NET):

'Model to pass data back upon    
Public Class RegistrationReportLine
    'checkboxes
    Public Readonly Property checkboxes() As String
        Get
            Return ""
        End Get
    End Property

    'registration keys
    Private _registrationKey As String
    Public Property registrationKey() As String
        Get
            Return _registrationKey
        End Get
        Set(ByVal value As String)
            _registrationKey = value
        End Set
    End Property

    'registration dates
    Private _registrationDate As String
    Public Property registrationDate() As String
        Get
            Return _registrationDate
        End Get
        Set(ByVal value As String)
            _registrationDate = value
        End Set
    End Property

    'amount Paid
    Private _amountPaid As String
    Public Property amountPaid() As String
        Get
            Return _amountPaid
        End Get
        Set(ByVal value As String)
            _amountPaid = value
        End Set
    End Property
End Class

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,189Questions: 1Answers: 10,412 Site admin
    Answer ✓

    You'd need to ask on SO or a VB.NET specific forum for help with the VB aspect. The client-side javascript looks okay - but to check, have a look in the Network tab of your browser's inspector and check the "Headers" for the data submitted to the server. What does it show?

    Allan

  • SportsavvyDevSportsavvyDev Posts: 3Questions: 1Answers: 0

    Hey Allan,

    It was an internal server error so very likely it is a VB.Net issue.

  • SportsavvyDevSportsavvyDev Posts: 3Questions: 1Answers: 0

    Hey Allan,

    It was an internal server error so very likely it is a VB.Net issue.

This discussion has been closed.