This is my first question so don't burn me at stake.
I have an API that I'm sending GET,POST,PUT,DELETE requests to using $resource. I have managed to make a GET and a POST request that do what they're supposed to, but whenever I try to send a PUT request I get a 404 error and an OPTIONS method sent instead.
Basically, my PUT request should be sent with same parameters that the POST has. When I send the seemingly same request over Postman extension, it works fine. I just have no idea how to replicate it via $resource.
plunker with issue: http://plnkr.co/edit/gJV4otD0XGKoQWOq3PdK (it's much more clear on plunkr than on this pasted piece of code by the way)
services.js
angular.module('starter.services',['ngResource']).factory('RESTCalls', function ($resource) {
return $resource('http://api.artistappz.com/api/v1/news/:urlkey/:id/x-app-id/3865f620590f40d493ec8d900b4c24d3/', null, {
update: {
method:'PUT'
},
save: {
method:'POST',
headers:{
'Content-Type':'application/x-www-form-urlencoded'
}
}
});
});
controllers.js
angular.module('starter.controllers',[]).controller('HomeController', ['$scope','RESTCalls', function ($scope, RESTCalls) {
$scope.RESTGet = function() {
RESTCalls.get(function (response) {
console.log(response);
});
};
$scope.RESTPost = function() {
RESTCalls.save('user_id=76A5FC91E4684FA1ACB2FDA645FD5B2C&title=dejan seme ti jebem krvavo&body=dejan&url=dejanurl',function(response) {
console.log('SUCCESS:');
console.log(response);
},function (responseerr) {
console.log('ERROR:');
console.log(responseerr);
});
};
$scope.RESTPut = function() {
///item/84173EF7D6FD4469B411057C72F53DE2
RESTCalls.get({urlkey:'item',id:'84173EF7D6FD4469B411057C72F53DE2'},function (response) {
var tempobject = response.data.news['84173EF7D6FD4469B411057C72F53DE2'];
tempobject.user_id = '76A5FC91E4684FA1ACB2FDA645FD5B2C';
RESTCalls.update({urlkey:'item',id:'84173EF7D6FD4469B411057C72F53DE2'},tempobject);
});
};}]);
postman PUT request https://i.sstatic.net/zLMUA.jpg
It's about 'HTTP access control'. Cause you request a resource to the different domain server. You can perform GET&POST request, but 'PUT' is different.
The browser side will send 'OPTIONS' request first. And then, upon "approval" from the server, sending the actual request with the actual HTTP request method.
If you inspect the network request in your plunker, you will notice it's 'OPTIONS' request instead of 'PUT'.