How to return success from ajax post in Node.js How to return success from ajax post in Node.js express express

How to return success from ajax post in Node.js


By returning JSON when you're expecting JSON

res.end('{"success" : "Updated Successfully", "status" : 200}');

and then

$.ajax({     ....    datatype: "json", // expecting JSON to be returned    success: function (result) {        console.log(result);        if(result.status == 200){            self.isEditMode(!self.isEditMode());        }    },    error: function(result){        console.log(result);    }});

In Node you could always use JSON.stringify to get valid JSON as well

var response = {    status  : 200,    success : 'Updated Successfully'}res.end(JSON.stringify(response));

Express also supports doing

res.json({success : "Updated Successfully", status : 200});

where it will convert the object to JSON and pass the appropriate headers for you automatically.