由于$ promise和$ resolve在JSON对象中,ngResource PUT返回错误的请求

时间:2014-12-08 19:11:38

标签: javascript json angularjs ngresource

我在过去的几个小时里一直在努力解决这个问题,而且每个教程都指向我已经实施的解决方案,但它并没有起作用。

基本上我的PUT请求会返回错误:

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

当我拦截请求时,我发现它包含$ promise和$ resolved数据元素:

> {"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}

这是有道理的,因为我使用的是ngResource对象 - 但每个教程都显示以下代码应该能够处理它,但它没有。

注意/编辑:如果我将JSON对象放在没有" $ promise"和" $已解决"通过外部程序(如Postman REST客户端)的元素然后它工作正常。

厂:

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

控制器(注意:执行4次更新,但没有一次更新,相同的错误请求的4倍):

.controller(' StockEditController',函数($范围,$日志,$ HTTP,$状态,$ stateParams,股票){

$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');
};
     

});

我现在真的很无能为力如何以正确的方式使用ngResource对象,以便我可以使用它来发布/发布到我的webservice。有什么想法吗?

谢谢!

编辑: Chrome网络输出:

回复标题

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

3 个答案:

答案 0 :(得分:0)

根据docs,您没有正确使用更新操作。

所以你的updateStock方法应该是:

$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 :(得分:0)

只要查看工厂定义及其与我的不同之处,您应该将工厂定义的第二个参数更改为数组:

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

不确定这是否是问题,但至少与角度文档的依赖注入定义明显不同:https://docs.angularjs.org/guide/di

如果没有,你可以在GET返回数据后打印出$ scope.stock的内容吗?

答案 2 :(得分:0)

我知道这个问题已经超级老了,但我偶然发现了同样的问题并发现了这个问题以及这个问题: http://www.scriptscoop2.com/t/fddc3f0a1f6f/angularjs-using-ngresource-for-crud-adds-extra-key-value-when-using-save.html

有1个答案解释了问题是由CORS(交叉起源)引起的。这意味着您的服务器端需要允许它。如果您使用的是Spring MVC,那么在控制器请求映射中添加org.springframework.web.bind.annotation.CrossOrigin注释就足够了:

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