Ajax method doesn't work in webforms visual basic.

Ajax method doesn't work in webforms visual basic.

DystDyst Posts: 8Questions: 3Answers: 0
edited May 6 in Free community support

Hello,

For reference, I'm using DataTable 2.0.6

I can't seem to make the DataTable call the function in aspx webforms in visual basic.

This method works:

$.ajax({
    type: 'POST',
    url: 'Index.aspx/TempGetEmployeeData',
    data: JSON.stringify({ Sqloption: 21 }),
    contentType: 'application/json; charset=utf-8',
    dataType: 'json', 
    success: function (response) {
        var parsedjsonChangeLogData = response.d; 
        $('#TempEmployeeTable').DataTable({
            data: parsedjsonChangeLogData 
        });
    }
});

but this method doesn't:

$('#TempEmployeeTable').DataTable({
    ajax: {
        type: 'POST',
        url: 'Index.aspx/TempGetEmployeeData',
        data: JSON.stringify({ Sqloption: 21 }),
        contentType: 'application/json; charset=utf-8',
        dataType: 'json'
    }
});

I also tried removing the type and change it to 'GET', removing contentType, and changing the dataType into text then used a success function to output it in the console.log but the output is the whole html file.

I tried debugging the function, but it doesn't go to the function itself.

Here is the function.

<WebMethod()>
Public Shared Function TempGetEmployeeData(sqlOption As Integer) As String
    Dim dt As New DataTable()
    Using connection As New SqlConnection(SQLConnStr)
        Using command As New SqlCommand("someStoredProcedure", connection)
            command.CommandType = CommandType.StoredProcedure
            command.Parameters.AddWithValue("@pOption", sqlOption)
            connection.Open()
            Using SQLresult As SqlDataReader = command.ExecuteReader()
                dt.Load(SQLresult)
            End Using
        End Using
    End Using
    Dim jsonResult As String = JsonConvert.SerializeObject(dt)
    Return jsonResult
End Function

Any help will be much appreciated. Thank you

This question has an accepted answers - jump to answer

Answers

  • DystDyst Posts: 8Questions: 3Answers: 0

    Here is the answer.

    $('#TempEmployeeTable').DataTable({
        ajax: {
            type: 'POST', 
            url: 'Index.aspx/TempGetEmployeeData', 
            contentType: 'application/json; charset=utf-8', 
            dataType: 'json',
            dataSrc: function (response) {
                return JSON.parse(response.d);
            }
        }
    });
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Ah! Your server is returning the cunning Microsoft JSON as a string wrapped in another JSON object. I've never understood why they did that! Glad to hear you got the solution though :).

    DataTables 2.1 is going to do that automatically (I've actually already committed that change - plenty more for 2.1 to be done though).

    Allan

Sign In or Register to comment.