Skip to content Skip to sidebar Skip to footer

Not Getting Response While Ajax Call In Jquery Data Table

I tried ajax call in separate page it's working fine but through jQuery data table I'm not getting response. API is in AWS. I tried though API end point with key. Problem is while

Solution 1:

You have the following variable containing the object you want to send to the server in your request:

var dataListNew = { "fromDate": "2021-01-01", "toDate": "2021-01-14"}; 

In your DataTables ajax call you are stringifying this variable:

data: JSON.stringify(dataListNew), // note you have a missing comma in your version

But JSON.stringify will cause the JSON object to be converted to a string - and then the data option will try to interpret that string as follows:

When data is passed as a string it should already be encoded using the correct encoding for contentType, which by default is application/x-www-form-urlencoded.

See the jQuery ajax documentation for details.

In your case, the string is not encoded correctly for that to work. Instead, you will get the URL-encoded data you are seeing:

0=%7B&1=%22&2=f&3=r&4=o&5=m&6=D&7=a&8=t&9=e&10=%22&11...

Instead, just pass the JSON object to the ajax data option:

data: dataListNew,

Now, the request payload will be constructed as per the following documentation guidelines:

When data is an object, jQuery generates the data string from the object's key/value pairs...

Now the payload looks like this in the request body:

fromDate=2021-01-01&toDate=2021-01-14

And therefore your server-side PHP logic should be able to read the request body in the usual way.

Post a Comment for "Not Getting Response While Ajax Call In Jquery Data Table"