I'm trying to put some data in mysql database using sails js API, Here is my data which I'm sending via ajax call
{"person":{"name":"Sahasrangshu Guha","address":"Dlf It Park Ii Block 1a, Plot No. Ii-F/1, Action A, Ericsson Kolkata","phoneNumber":"9830612244"}}
Here is the ajax call header
{ 'Content-Type': 'application/json', 'responseType': 'json' }
My sails js controller is as follows
module.exports = {
addPerson: function (req, res) {
if (req.method == 'POST' && req.param('person', null) != null) {
console.log(req.param('person'));
Person.create(req.param('person'), (error, person) => {
if(error) {
res.send('Error:Sorry!Something went Wrong');
} else {
res.send('Successfully Created!');
}
});
// Error handling
// if (err) {
// res.send('Error:Sorry!Something went Wrong');
// } else {}
// res.send('Successfully Created!');
// //res.redirect( ‘person/view/’+model.id);
// }
}
else {
res.send('Error:Sorry!Data Wrong');
}
}
}
And my sails js model looks like below
module.exports = {
tableName: 'person',
primaryKey: 'id',
attributes: {
name: {
type: 'string',
required: true
},
address: {
type: 'string',
required: true
},
phoneNumber: {
type: 'string',
required: true
}
}
};
I'm alaways having the following error whenver I'm making a POST request to my sails js API
{error: SyntaxError: Unexpected token E in JSON at position 0 at JSON.parse ()
Any idea to solve this? I have tried sending object rather than JSON data but the same error pursues.