我是 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/