Reputation: 76
I have an AngularJS front-end talking to a Node/Express/MySQL backend, and all requests work fine apart from a POST request, here's my Angular Code:
var Data = $resource('http://127.0.0.1:9001/api/data_user');
var newData = new Data({'data_id': data_id});
newData.test = 'whatever';
newData.$save(function (data) {
console.log(data);
});
And I've taken my usual debugging steps of removing all code until something works, however even this is not working (from client):
app.post('/api/:table', function (req, res) {
res.send(200);
});
I previously had a MySQL update statement in there, which was being executed properly (with error checking etc), however the front-end is just waiting for a response to come:
(Chrome Dev tools): data_user /api POST (pending) Pending
The annoying thing about this is that a CURL works fine:
$ curl -i -X POST http://127.0.0.1:9001/api/data_user --data '{}' -H "Content-Type: application/json"
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/plain
Content-Length: 2
Date: Mon, 31 Mar 2014 09:54:21 GMT
Connection: keep-alive
Has anyone got any ideas at all? Can post more code if required
Upvotes: 3
Views: 2827
Reputation: 168
Are you viewing your page as 127.0.0.1 or as localhost? You may be running into CORS CORS. This would explain why a cURL works but a POST request from your page does not work if you are viewing your page as "localhost" but are posting to 127.0.0.1
Upvotes: 2