javascript - AngularJS $http.delete 破坏了 "TypeError: Cannot assign to read only property ' 方法’..”

标签 javascript angularjs

我是 JavaScript 和 AngularJS 新手。我正在尝试构建一个非常简单的CRUD应用程序。这是我的 Controller 的代码:

(function() {
    var app = angular.module('form.user', []);

    app.directive('formusergui', [function(){
        return {
            restrict: 'E',
            templateUrl: './js/views/user.form.html',
            controller: function($scope, $http) {

                this.formToBean = function() {
                    var ent = {};
                    ent.id = null;
                    ent.name = $scope.name;
                    ent.desc = $scope.desc;
                    ent.reg = Date.now; 
                    ent.linkImgProfile = null;
                    ent.type = null;
                    return ent;
                };

                this.create = function() {      
                    $http.post('./services/users', JSON.stringify(this.formToBean()))
                    .success(function(data, status, headers, config) {
                        $scope.esito = "success: " + JSON.stringify(data);
                    })
                    .error(function(data, status, headers, config) {
                        $scope.esito = "error: " + JSON.stringify(data);
                    });
                };

                this.delete = function() {
                    $http.delete('./services/users', JSON.stringify(this.formToBean()))
                    .success(function(data, status, headers, config) {
                        $scope.esito = "success: " + JSON.stringify(data);
                    })
                    .error(function(data, status, headers, config) {
                        $scope.esito = "error: " + JSON.stringify(data);
                    });
                };
                ...
            },
            controllerAs: 'userForm'
        };
    }]);
})();

formToBean 函数通过表单收集数据。由于 create 函数运行良好(调用 REST 服务),delete 函数因以下错误而中断:

TypeError: Cannot assign to read only property 'method' of {"id":null,"name":"john","desc":"doe","linkImgProfile":null,"type":null}

我发现了关于此类错误的类似问题,但我仍然不明白出了什么问题。

最佳答案

对于$http中的delete方法,第二个参数是一个配置,在你的请求中不是配置相关的数据,简单的传递json对象,引用$http.delete的api

https://docs.angularjs.org/api/ng/service/ $http#删除

$http.delete('./services/users/:idtodelete')
   .success(function(data, status, headers, config) {
        $scope.esito = "success: " + JSON.stringify(data);
    });

关于javascript - AngularJS $http.delete 破坏了 "TypeError: Cannot assign to read only property ' 方法’..”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28022251/

相关文章:

css - (AngularJS) 整个网站只少了一个文件

angularjs - 如何检测在 AngularJS 中完成的所有图像加载

angularjs - 类型错误 : Cannot call method 'replace' of undefined

php - 如何将 jQuery 语句应用于在运行时加载的元素?

javascript - Pusher 未在 Heroku Rails 应用程序上触发,但在 localhost :3000 上工作正常

javascript - 群组连接后爆炸回复

javascript - AngularJS 密码确认 noMatch 工作但表单 $ 无效?

javascript - 检测不可滚动 div 内的滚动

javascript - 如何从 Promise 中返回值

javascript - Switch case 传递多个值