最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Data table post request params not working - Stack Overflow

programmeradmin0浏览0评论

I am new to AngularJS and DataTables. I am working on a project using an AngularJS front-end and a Rails back-end.

I am trying to use DataTables in my project using AJAX POST method but DataTable AJAX POST method is sending data params constants.

When I am using a normal AJAX POST request it works fine. I don't know why DataTables POST AJAX is not working.

Please help me to solve this problem.

Using AJAX call in AngularJS

$.ajax({
   data: JSON.stringify({
       "key1": "value1",
       "key2": "value2"
   }),
   headers: "application/json; charset=utf-8",
   success: function(data){
        console.log(data);
   },
   error: function(){
       console.log("error");
   },
   type: "POST",
   url: "http://localhost:3000/api"
 });

This code AJAX POST.

It gives the correct success response.

DataTables POST request

$scope.studentList = {
   bFilter: false,
   paging:   false,
   bRetrieve : true,
   ajax: {
       data: JSON.stringify({
       "key1": "value1",
       "key2": "value2"
   }),
   headers: "application/json; charset=utf-8",
   success: function(data){
        console.log(data);
   },
   error: function(){
       console.log("error");
   },
   type: "POST",
   url: "http://localhost:3000/api"
   processData: false,
 },
 columns: [
     {}]
};

It throws the following error on the server-side. Is there any mistake in my code?

Error occurred while parsing request parameters.

Contents:

0=%7B&1=%22&2=s&3=t&4=a&5=n&6=d&7=a&8=r&9=d&10=&11=i&12=d&13=%22&14=%3A&15=4&16=0&17=%2C&18=%22&19=s&20=e&21=c&22=t&23=i&24=o&25=n&26=&27=i&28=d&29=%22&30=%3A&31=8&32=%2C&33=%22&34=y&35=e&36=a&37=r&38=%22&39=%3A&40=2&41=0&42=1&43=6&44=%2C&45=%22&46=m&47=o&48=n&49=t&50=h&51=%22&52=%3A&53=2&54=%2C&55=%22&56=d&57=a&58=t&59=e&60=%22&61=%3A&62=1&63=8&64=%7D

Even though I am using processData: false option it still throws the following error on the server-side.

Error occurred while parsing request parameters.

Contents:

[object Object]

Please help me to solve this issue.

I am new to AngularJS and DataTables. I am working on a project using an AngularJS front-end and a Rails back-end.

I am trying to use DataTables in my project using AJAX POST method but DataTable AJAX POST method is sending data params constants.

When I am using a normal AJAX POST request it works fine. I don't know why DataTables POST AJAX is not working.

Please help me to solve this problem.

Using AJAX call in AngularJS

$.ajax({
   data: JSON.stringify({
       "key1": "value1",
       "key2": "value2"
   }),
   headers: "application/json; charset=utf-8",
   success: function(data){
        console.log(data);
   },
   error: function(){
       console.log("error");
   },
   type: "POST",
   url: "http://localhost:3000/api"
 });

This code AJAX POST.

It gives the correct success response.

DataTables POST request

$scope.studentList = {
   bFilter: false,
   paging:   false,
   bRetrieve : true,
   ajax: {
       data: JSON.stringify({
       "key1": "value1",
       "key2": "value2"
   }),
   headers: "application/json; charset=utf-8",
   success: function(data){
        console.log(data);
   },
   error: function(){
       console.log("error");
   },
   type: "POST",
   url: "http://localhost:3000/api"
   processData: false,
 },
 columns: [
     {}]
};

It throws the following error on the server-side. Is there any mistake in my code?

Error occurred while parsing request parameters.

Contents:

0=%7B&1=%22&2=s&3=t&4=a&5=n&6=d&7=a&8=r&9=d&10=&11=i&12=d&13=%22&14=%3A&15=4&16=0&17=%2C&18=%22&19=s&20=e&21=c&22=t&23=i&24=o&25=n&26=&27=i&28=d&29=%22&30=%3A&31=8&32=%2C&33=%22&34=y&35=e&36=a&37=r&38=%22&39=%3A&40=2&41=0&42=1&43=6&44=%2C&45=%22&46=m&47=o&48=n&49=t&50=h&51=%22&52=%3A&53=2&54=%2C&55=%22&56=d&57=a&58=t&59=e&60=%22&61=%3A&62=1&63=8&64=%7D

Even though I am using processData: false option it still throws the following error on the server-side.

Error occurred while parsing request parameters.

Contents:

[object Object]

Please help me to solve this issue.

Share Improve this question edited Oct 31, 2016 at 19:17 Carlos Ramirez III 7,43430 silver badges35 bronze badges asked Jan 20, 2016 at 13:48 Sasikala JSasikala J 5265 silver badges14 bronze badges 1
  • Check out this: datatables/forums/discussion/26282/… – ge333 Commented Jan 14, 2022 at 17:58
Add a ment  | 

4 Answers 4

Reset to default 1

In dataTables you can not pass a string in data, you need a function:

var dataObject = {
  "key1": "value1",
  "key2": "value2"
};

var table = $('#exampleTable').DataTable({
  ajax: {
    "url": "urlExample.",
    "type": "POST",
    "data": function(d) {
      $.extend(d, dataObject);
      return JSON.stringify(d);
    },
    "dateType": "json",
    "contentType": "application/json; charset=utf-8"
  }
});

Try removing JSON.stringify. data table or ajax internally handles JSON parsing/stringification.

Use this instead :

$('#example').dataTable({
    "ajax": {
        "url": "data.json",
        "contentType": "application/json",
        "type": "POST",
        "data": function (d) {
            return JSON.stringify(d);
        }
    }
});
var table = $('#datatable').DataTable({
// "processing": true,
"serverSide": true,
  "ajax": 'controller/documents/getDocExpAjax',
  "type": 'POST',
  "data": function (d) {
     return JSON.stringify(d);
  },
  "dateType": "json",
  "contentType": "application/json; charset=utf-8"
});
发布评论

评论列表(0)

  1. 暂无评论