I need to send the data from an HTML form using a javascript function to a node.js server running at the same address. For the record, i have a website hosted on my Raspberry PI, using a custom domain. This is the javascript function:
$.post("http://www.website.com:2052/form", formData, function(data){
...
console.log("POST SUCCESS");
...
});
This is the code for my node.js server:
app.post('/form', function(req, res){
....
console.log('POST - body: ' + JSON.stringify(req.body));
res.send({result:"success"});
....
});
When i send the form data to the server, i receive it on the server, but i get this on website-side:
Failed to load http://www.website.com:2052/form: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.website.com' is therefore not allowed access.
I understand that i need to add the access control header in the response, but considering they are on the same "ip", why is it needed? Or how can i make the node.js server actually appear on the same domain? Thanks
app.use(cors())npmjs.com/package/cors