I'm building a web app using Node.js and MongoDB (Mongoose),
Shorty- a submit button with POST method making an HTTP GET-request which save data in mongoose and then rendering the data in the homepage.
I have tried to use ajax in order to load only the relevant part of the page that rendered each call but unfortunately for some reason it does not work.
What am I missed here?
The Submit button -
<button class="btn btn-primary" onclick="loadAjax" type="submit">Go</button>
The mongoose logic -
app.get("/system", function(req, res) {
if(req.isAuthenticated()){
Asin.find({},function(err,foundData){
if(foundData.lengh === 0){
Asin.insertMany(defaultAsin,function(err){
if(err){
console.log(err);
}else{
console.log("default mode apply");
}
});
res.redirect("/system");
}else{
res.render("system",{asinNumber:asin,imageUrl:img1 + img2,productUrl:prodUrl+asin,amzAsinData:foundData});
}
});
}else{
res.redirect("/login");
}
});
The ajax code -
$(document).ready(function() {
function loadAjax(str){
if (str !== ""){
$.ajax({
url: "/",
method: "POST",
contentType: "application/json",
data: JSON.stringify({defaultAsin: str}),
success: function(result){
$("#ajax-meth").html(result.html);
}
});
}
};
})(jQuery);