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'},
]
});
}
});
});
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 formatwhat? those are two unrelated things.localhost:3000/user_listin 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.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?