1

I've been struggling with this problem for the last few hours, and every tutorial points toward the solution that I have implemented but it doesn't work.

Basically my PUT request returns an error:

PUT http://localhost:8083/stockapi/rest/stocks/5485cba248673a0dd82bb86f 400 (Bad Request)

When I intercept the request, I see that it contains a $promise and $resolved data element:

> {"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 makes sense since I'm using the ngResource object -- but every tutorial shows that the following code should be able to handle it, but it doesn't.

Note/edit: if i PUT the JSON object without the "$promise" and "$resolved" elements through an external program (such as Postman REST client) then it works 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: doing 4 updates but none of them work, 4 times the same Bad Request):

.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 really clueless right now how to use the ngResource object in the correct way so that I can use it to put/post to my webservice. Any ideas?

Thanks!

EDIT: 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
6
  • To me it rather looks like that the server controller is not configured to accept PUT request (perhaps it accepts POST instead). Could you please check and copy & paste log from Chrome console (network tab) to show what network data is acually sent? Dec 8, 2014 at 19:15
  • The server accepts PUT request. When I remove the $promise and $resolved elements from the JSON object and use a REST client such as PostMan Chrome Extension then I can PUT (=update) the object without problem. Will have a look at the Chrome console network tab as well.
    – mickske
    Dec 8, 2014 at 19:36
  • 1
    Ok, then please show us what your client sends to the server (network tab). I bet the answer/clue is there. Dec 8, 2014 at 20:01
  • Added now in main post. Thanks for help.
    – mickske
    Dec 8, 2014 at 20:44
  • Can you hit 'view source' on the request payload so we can see the whole thing?
    – Scottux
    Dec 8, 2014 at 20:49

3 Answers 3

0

According to the docs you are not quite using the update action correctly.

So really your updateStock method should be:

$scope.updateStock=function(stock) {
    Stock.update({id: stock.id}, stock);

    Stock.update({id: $scope.stock.id}, $scope.stock);   //not sure why you have 2 calls here

    $state.go('stocks');
};
1
  • Hi. I have two calls just because I tried two different ways. Unfortunately adding the id in the update method doesn't change anything. The functionality remains the same but I keep getting Bad Request since they $promise and $resolve elements are still on the JSON.
    – mickske
    Dec 9, 2014 at 6:43
0

Just looking at the factory definition and how it differs from mine, you should change that second parameter of factory definition into array to something like this:

.factory('Stock',[$resource, function($resource){
    return $resource('http://localhost:8083/stockapi/rest/stocks/:id',
        { id: '@id' },{
            update: { method: 'PUT' },
            show: { method: 'GET' }
    }); 
}]);

Not sure if this is the issue but that is at least significantly different to angular docs' definition on dependency injection here: https://docs.angularjs.org/guide/di

If not, could you also print out the contents of $scope.stock after the GET returns the data there?

1
  • Sorry for the slow response, been quite busy. If I run it like this I get an error message: "Uncaught ReferenceError: $resource is not defined" -- however, it does seem that it should work like that according to the docs you linked. I will investigate further later tonight. Thanks for the response.
    – mickske
    Dec 10, 2014 at 16:34
0

I know this question is super old, but I stumbled upon the same issue and found this SO question as well as this question: http://www.scriptscoop2.com/t/fddc3f0a1f6f/angularjs-using-ngresource-for-crud-adds-extra-key-value-when-using-save.html

There was 1 answer which explained that the issue is coming due to CORS (cross origins). Which means that your server side needs to allow it. If you are using Spring MVC it would be enough to add the org.springframework.web.bind.annotation.CrossOrigin annotation at the controllers request mapping:

@CrossOrigin
@RequestMapping(value = "/product/{id}", method = RequestMethod.POST)
@ResponseBody
public void editProduct(@PathVariable("id") final long id, @RequestBody final ProductDto productDto) {
    // your code
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.