作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 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/
我是一名优秀的程序员,十分优秀!