1

我在这里的理解或代码中遗漏了一些东西......

我有一个非常基本的 CRUD 应用程序来列出/添加/编辑/删除类别,当我将表上的主键定义为“id”时它工作正常,但是当我将数据库列重命名为“categoryId”时, PUT url 不包含键值,我最终会因未处理的路径出现 404 错误...

堆:

用于 RESTful 服务的带有 Slim PHP 的 IIS 7.5

// Route excerpt
...
          when('/categories',       {templateUrl: 'partials/categoryList.html',   controller: CategoryListCtrl}).
          when('/categories/new',   {templateUrl: 'partials/categoryDetail.html', controller: CategoryNewCtrl}).
          when('/categories/:id',   {templateUrl: 'partials/categoryDetail.html', controller: CategoryEditCtrl}).
...

angular.module('myServices', ['ngResource']) 
.factory('Category', function($resource){ 
    return $resource('../api/index.php/categories/:id', {id:'@categoryId'}, 
        { update: {method:'PUT' } } 
     ); 
})




// WORKS when attribute is 'id'
.factory('Category', function($resource){
        return $resource('../api/index.php/categories/:id', {id:'@id'}, 
            { update: {method:'PUT' } }
        );
    })


// FAILS with 404 when attribute is 'categoryId'
.factory('Category', function($resource){
        return $resource('../api/index.php/categories/:id', {id:'@categoryId'}, 
            { update: {method:'PUT' } }
        );
    })

当然代码中还有很多其他地方改名了,但是我看到的效果好像和ngResource有关。

第一种方法产生一个有效的 URL...

 categories/1?description=null&name=Observation&report=null
/api/index.php

第二个产生这个......

 categories?categoryId=1&description=null&name=Observation&report=null
/api/index.php

由于路径格式错误,出现 404 错误。

是否需要另一个参数(或指令或其他东西)来获取要在 URL 中使用的重命名属性?

4

1 回答 1

1

我用第二个资源测试了 put,它按预期工作。我可以重现的唯一方法是发出 GET 而不是 PUT。如果您打开此演示并查看网络选项卡(chrome/etc),您可以看到 put 和 get 产生了什么。

http://plnkr.co/edit/tI9KpqDp49pXuJJVjivm?p=preview

GET 生成查询字符串参数化数据:

GET 产生:

Request URL:http://run.plnkr.co/api/index.php/categories?categoryId=123
Request Method:GET

PUT 产生:

Request URL:http://run.plnkr.co/api/index.php/categories/123
Request Method:PUT

代码:

   var test= $resource('../api/index.php/categories/:id', {id:'@categoryId'}, 
        { update: {method:'PUT' } }
    );

     var test2= $resource('../api/index.php/categories/:id', {id:'@categoryId'}, 
        { get: {method:'GET' } }
    );

    test.update({categoryId:123});
    test2.get({categoryId:123});
于 2013-05-13T22:48:47.557 回答