I have been trying to send the data I receive from mysql using Nodejs and try to receive it with Ajax. However, for some reason what I receive is always undefined.
The problem is either the way I am sending it or the way I receive it. Here is my question in more detail and code:
I have a form/ button.
<form id = "loadform" action= "/loaded" method ="POST">
<input placeholder = "Zip Code(Optional)" name = 'zipcode'>
<button id= "datasubmit">Load Data</button>
</form>
When clicked I expect this query to be executed. And it receives the correct data and prints out to the console correctly.
app.post('/loaded', function(req,res){
connection.query("SELECT * FROM fmpDBTable", function(error,rows,fields){
if(error){
console.log('Error in the query')
}
else{
console.log('successful query')
console.log(rows[0].Latitude)
console.log(rows.length)
res.status(204).json(rows);
}
})
})
and I expect to have the following Ajax call executed when I click the submit button
$('#datasubmit').click(function(e){
//alert("jhhfdsfs")
$.ajax({
url: 'loaded/',
type: 'POST',
dataType: 'json',
success: (data) =>{
alert("checkpoint1")
console.log(data)
}
})
})
Great so far. However, the data that I am seeing is undefined (when I check it inspector console). Alert works btw
Where am I going wrong? I have checked bunch of similar threads and still not sure where I am going wrong