Tuesday 31 January 2017

What to use to send data back to browser for AJAX call

I have an AJAX call using express and jquery on the front end, it saves a field to an array in my Mongo db and that works. However I cannot figure out how to send the data back to the AJAX call to populate the page.It does however output that res.send and takes over the page with the output object. Do I need to wrap that res.send in something or is that the wrong method to use?Here is my node route:router.route("/user/categories/:id") .all(function(req,res,next){ console.log("I see the Ajax request Here"); isLoggedIn; next(); }) .post(function (req,res){ console.log("AJAX call Hit the server here now"); User.findByIdAndUpdate(req.user.id ,{$push: {categories: req.body.user.category}} ,{save: true, upsert: true, new: true} ,function(err, category){ //Need to return categories to the front end res.send({category}); }) }) Here is my AJAX:$("#add_category_button").on('submit',function (event){ event.preventDefault(); event.stopPropagation(); var thisForm = $(this) ,term = thisForm.find("input[name='user[category]']").val $.ajax({ url: $("add_category_form").attr("action"), type: "POST", contentType: "application/json", data: JSON.stringify(thisForm), dataType: json, done: function (result) { alert("successful"); //console.log(result.categories); }, fail: function (fail){ console.log(fail); } }); }); I would think the Alert would trigger, but it does not.

Submitted February 01, 2017 at 03:47AM by illcrx

No comments:

Post a Comment