1

I am sending data using Nodejs res.end() in JSON format, and I am also setting headers as application/json and but I am not getting the data in the success function of jquery ajax call, instead I am getting the data directly in JSON format, but I want it to come in the success function of jquery ajax call as I have to display them in datatable. I even tried res.render() and res.send() but not able to get it. please help!!

Nodejs code

router.get('/user_list',(req,res,next)=>{
   let new_password = generator.generate({
        length: 16,
        numbers: true,
        symbols: true,
        uppercase: true,
        strict: true,
        exclude: ["(",")","[","]",":","-",",","{","}","<",">",".","^","/"]
      });
  connection.query("update user, client set user.status = client.status where user.client_id = client.id",(error,result)=>{
    if(error){
      console.log(error);
    }else{
      connection.query('select user.username,user.email,user.msg_id,user.status,system.name from user inner join system on user.system_id = system.id; select id, name from client;',(error,result)=>{
        if(error){
          console.log(error);
        }else{
          let value = {values: result,new_password};
          //res.json(value);
         // res.render('user_list', {user_data: value});
          res.writeHead(200, { 'Content-Type': 'application/json' });res.end(JSON.stringify(value));
        }
      })
    }
  })
})

JQuery Code

$(document).ready(function(){
      $.ajax({
        url:'user_list',
        type:'GET',
        //dataSrc: 'user_list',
        datatype:'json',
        success: function(data){
          console.log(data);
          $('#example').dataTable({
            data:data,
            columns: [
              {data:'values[0][1].username'},
              {data:'values[0][1].name'},
              {data:'values[0][1].email'},
              {data:'values[0][1].msg_id'},
            ]
          });
        }
      });
    });

12
  • 2
    1. NEVER EVER post code as image for obvious reasons 2. just use res.send(some_object) and express will set the header appropriately 3. but i am not getting the data in the success function of jquery ajax call, instead i am getting the data directly in json format what? those are two unrelated things. Commented Sep 27, 2018 at 7:58
  • i tried res.send() but i am not able to get the response in success function of jquery ajax call.@ChrisG Commented Sep 27, 2018 at 8:35
  • 1
    Any errors in the console? Note that since this is a GET request and you don't have any parameters, you can simply go to localhost:3000/user_list in the browser and you should see the JSON. Does that work? Also, please edit your question so it contains the actual code, or this question will likely get closed. Commented Sep 27, 2018 at 8:45
  • There is no error in console and also i get the response that way in the browser but i don't want that, i want it in the success function so that i populate the data in datatable.@ChrisG Commented Sep 27, 2018 at 9:09
  • But you ARE getting it in the success function. console.log(data) is inside the success function! I'm starting to think that this has nothing to do with ajax and is just about the .dataTable() call. When the data is logged in the console, is it just a long String? Commented Sep 27, 2018 at 9:50

1 Answer 1

1

Please try this so we can narrow down the problem:

router.get('/user_list', function (req, res) {
    const list = [
        { id: 0, name: "Alice"},
        { id: 1, name: "Bob"}
    ];
    res.send(list);
});

In your index/script:

$(document).ready(function () {
    $.getJSON('user_list').then(res => {
        console.log(res);
    });
});

This should display the Array in the console.

Sign up to request clarification or add additional context in comments.

5 Comments

I commented everything and just used your code but still getting the response in the browser and this also didn't print anything in the console. Screenshot of the response :imgur.com/a/Lqp9JoF
Ok, so now go to the page that contains the jQuery code ($.getJSON) and check the browser console. (also, seeing just the JSON is obviously what's supposed to happen when you visit /user_list via the address bar... ; we're troubleshooting your code and have just successfully excluded the backend as the source of the problem)
i already posted an url in above comment which contains the screen shot of the ouput. If that's how its supposed to work then how am i supposed to make an ajax call and get the data in front end and display it?
That was only to check whether the backend works as it should. The next step is to make the same request via AJAX.
yeah at first when i posted the question i was making an ajax call to backend and receiving the data in the browser and not in the success function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.