Ajax.data - Invalid JSON Primitive Error

Ajax.data - Invalid JSON Primitive Error

jespanajespana Posts: 5Questions: 2Answers: 0

Hi all,

I am using version, 1.10.3, of DataTables and have a question about the ajax.data option. I have looked at the examples in the documentation. (https://datatables.net/reference/option/ajax.data) But, I can't seem to clear the Invalid JSON primitive error. Here is a sample of my code:

$('#grdTournamentList').DataTable({
'ajax': {
'type': 'POST',
'contentType': 'application/json; charset=utf-8',
'url': '@Url.Action("MethodAByName", "QuickStart")',
'data': {"name":"jun"},
'dataType': 'json',
'dataSrc': '',
//'cache': false
}
});

I've attempted every variation of the {name:jun} data parameter that I can think of and it always fails with the same error, Invalid JSON Primitive Error: name. I know my web method call is working correctly, because I can call it as follows using jQuery.ajax() directly. Please note this is a ASP.NET MVC application.

$.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
url: '@Url.Action("MethodAByName", "QuickStart")',
data: '{name:"juni"}',
dataType: 'json',
success: function (data, textStatus, jqWHR) {
debugger;
alert(data);
}
});

I recognize that in my "successful" ajax example, the data is being passed in as a JSON string. Anyway of mimicking that behavior with the ajax.data option from the Datatable?

Thank you in advance.

Answers

  • jespanajespana Posts: 5Questions: 2Answers: 0

    Hi all,

    I found the solution and am posting it here in case someone else encounters the issue.

    'ajax': { 
        'type': 'POST',
        'contentType': 'application/json; charset=utf-8',
        'url': '@Url.Action("MethodAByName", "QuickStart")',
        'data': function (d) {
                            return "{name:'juni'}";
                        },
                        'dataType': 'json',
                        'dataSrc': '',
                        'cache': false
                    }
    
This discussion has been closed.