I've been trying to solve this issue for hours now, and despite following various tutorials, I still can't get it to work.
The problem lies with my PUT request which returns a 400 (Bad Request) error:
PUT http://localhost:8083/stockapi/rest/stocks/5485cba248673a0dd82bb86f 400 (Bad Request)
Upon inspecting the request, I noticed that it includes $promise and $resolved data elements:
> {"id":"5485cba248673a0dd82bb86f","name":"iShares ESTOCK DivXXX","ticker":"AMS:IDVY","url":"https://www.google.com/finance?q=AMS%3AIDVY&ei=F5BxVLiCB8GlwQPJ1YD4DQ","currency":"EUR","currentPrice":19.81,"currentPriceInEuro":19.81,"lastModified":1418054562234,"historyStockPrices":[{"timestamp":1418054562234,"price":19.81}],"$promise":{},"$resolved":true}
This behavior seems to be linked to the ngResource object, as indicated by several tutorials suggesting that the code below should address it, but it doesn't:
Update/edit: when I send the JSON object without the "$promise" and "$resolved" elements via an external tool like Postman REST client, it works perfectly fine.
Factory:
.factory('Stock',function($resource){ return $resource('http://localhost:8083/stockapi/rest/stocks/:id', { id: '@id' },{ update: { method: 'PUT' }, show: { method: 'GET' } }); });
Controller (note: despite attempting 4 updates, none of them are successful - resulting in 4 Bad Requests):
.controller('StockEditController',function($scope,$log,$http,$state,$stateParams,Stock){
$scope.stock = Stock.get({id:$stateParams.id}); $scope.updateStock=function(stock) { Stock.update(stock); stock.$update(); Stock.update($scope.stock); $scope.stock.$update(); $state.go('stocks'); };
});
I'm currently at a loss regarding how to correctly utilize the ngResource object to make PUT/POST calls to my web service. Any suggestions would be greatly appreciated!
Thank you!
UPDATE: Chrome network output:
Response header
Remote Address:[::1]:8080
Request URL:http://localhost:8080/stockapi/rest/stocks/5485cba248673a0dd82bb86f
Request Method:PUT
Status Code:400 Bad Request
Request Headersview parsed
PUT /stockapi/rest/stocks/5485cba248673a0dd82bb86f HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 355
Pragma: no-cache
Cache-Control: no-cache
Accept: application/json, text/plain, */*
Origin: http://localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: http://localhost:8080/stockapi/
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Request Payloadview parsed
{"id":"5485cba248673a0dd82bb86f","name":"iShares ESTOCK DivXXXYYY","ticker":"AMS:IDVY","url":"https://www.google.com/finance?q=AMS%3AIDVY&ei=F5BxVLiCB8GlwQPJ1YD4DQ","currency":"EUR","currentPrice":19.81,"currentPriceInEuro":19.81,"lastModified":1418054562234,"historyStockPrices":[{"timestamp":1418054562234,"price":19.81}],"$promise":{},"$resolved":true}
Response Headersview parsed
HTTP/1.1 400 Bad Request
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Language: en
Content-Length: 968
Date: Tue, 09 Dec 2014 06:36:24 GMT
Connection: close