0

I'm able to see the response sent from the server in the network tab when I 'inspect' the page. However, console doesn't log the response in 'success'. Node.js as the backend. Following is the code :

function getFilteredData(){

    $(function( $ ){
        console.log("getting table data");

        var filters ={};     
        //inserted some data in filters
        $.ajax({
            type : 'POST',
            url : "query/get-filtered-data",
            dataType : 'application/json',
            async: "true",
            data:JSON.stringify(filters),
            contentType: 'application/json; charset=utf-8',
            success : function(data){
                console.log(data);
                // doesn't log anything. 
                //server response is visible in networks section of 'inspect' in browser.

            },
            failure: function(data){
                alert('got an error');
            }
        });
    });

}

I send 'filters' as the data to the server side. There, I use these filters to query a mongodb collection using mongoose.

router.post('/get-filtered-data', bodyParser.json(), function (req, res) {

  console.log(req.body);
  var filters = req.body;

  // authorModel is my mongoose model.
  authorModel.find({})
    .then(function (doc) {
      console.log(doc);    //prints the correct response
      res.send(doc);      
    });
});

Is there anything wrong with the code or am I missing something? Thanks a lot in advance.

EDIT: Added the function where I make the ajax call. This function is in a in the HTML file.

UPDATE Thanks to @Quentin, it now works. Changed my datatype in the ajax call to dataType:'json'.

0 Answers0