JQuery DataTable passes parameter to ajax call to asp.net. Invalid JSON primitive

Below is the java script code to populate my HTML table with server data, and I am using JQuery DataTables for this purpose.

function LoadData(result) {
$('#example').DataTable({
"ajax": {
   "dataType": 'json',
   "contentType": "application/json; charset=utf-8",
   "type": "POST",
   "url": "index.aspx/Risky",
   "data": function (d) {
    return JSON.stringify( d )
    //return JSON.stringify(result);
    // d.extra_search = result;      
    //"extra_search": result

   },
   "dataSrc": function (json) {
       return $.parseJSON(json.d);
   }
},
"columns": [
    { "data": "Prctice_Group_Risk_No" },
    { "data": "Practice_Group" },
    { "data": "Risk_Category" },
 ]
});
}

Below is my code for the description of the web method

[WebMethod]
[ScriptMethod]
public static string Risky()
{
  return JsonConvert.SerializeObject(riskList);
}

So far, his work is beautiful, my web method is being called, and my HTML table is populating.

But my problem is that I want to pass the result variable as a parameter to this ajax call so that my web method gets it and returns me certain data based on this parameter.

https://datatables.net/reference/option/ajax.data , ajax-, . java script , , , : " JSON" 500 Firebug. Firebug, "extra_search = 123"

, , , - json. , .

.

+1
2

:

function LoadData(result) {
   $('#example').DataTable({
        "ajax": {
                "url": "index.aspx/Risky",
                "data": function(d) {
                        d.param1  = 'param1';
                }
        },
        "aoColumns": [
            { "data": "Prctice_Group_Risk_No" },
            { "data": "Practice_Group" },
            { "data": "Risk_Category" }
        ]
    });
}
0

@Sanjay Kumar N S

https://datatables.net/forums/discussion/24546/ajax-data-invalid-json-primitive-error

. , JSON, "Invalid JSON Primitive"

ajax, DataTable

function LoadData(result) {
$('#example').DataTable({
"ajax": {
"dataType": 'json',
"contentType": "application/json; charset=utf-8",
"type": "POST",
"url": "index.aspx/Risky",
"data": function (d) {
 return "{FileName:" + result+ "}";

},
"dataSrc": function (json) {
   return $.parseJSON(json.d);
}
},
"columns": [
{ "data": "Prctice_Group_Risk_No" },
{ "data": "Practice_Group" },
{ "data": "Risk_Category" },
]
});
}
+1

Source: https://habr.com/ru/post/1615393/


All Articles