gpt4 book ai didi

javascript - AngularJS $http.delete 中断了 "TypeError: Cannot assign to read only property ' 方法' .."

转载 作者:行者123 更新时间:2023-11-29 17:01:06 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com