gpt4 book ai didi

javascript - Angularjs CRUD 删除问题(在 DELETE 方法中调用 GET)

转载 作者:太空宇宙 更新时间:2023-11-04 16:28:55 25 4
gpt4 key购买 nike

我正在尝试使用 angularjs 客户端来使用我的 Spring Rest 服务 link

创建、更新和读取部分正在工作。当我尝试删除时,它显示此错误。

Error: [$resource:badcfg] Error in resource configuration for action get. Expected response to contain an object but got an array (Request: GET http://localhost:8080/SpringRestExample/employee)

为什么我在 DELETE 方法中收到 GET 请求?

employee_service.js

'use strict';

App.factory('Employee', ['$resource', function ($resource) {
return $resource(
'http://localhost:8080/SpringRestExample/employee/:id',
{id: '@employeeId'},
{
update: {
method: 'PUT'
}

}
);
}]);

employee_controller.js

'use strict';

App.controller('EmployeeController', ['$scope', 'Employee', function($scope, Employee) {
var self = this;
self.employee= new Employee();

self.employees=[];

self.fetchAllEmployees = function(){
self.employees = Employee.query();
};

self.createEmployee = function(){
self.employee.$save(function(){
self.fetchAllEmployees();
});
};

self.updateEmployee = function(){
self.employee.$update(function(){
self.fetchAllEmployees();
});
};

self.deleteEmployee = function(identity){
var employee = Employee.get({employeeId:identity}, function() {
employee.$delete(function(){
console.log('Deleting employee with id ', identity);
self.fetchAllEmployees();
});
});
};

self.fetchAllEmployees();

self.submit = function() {
if(self.employee.employeeId==null){
console.log('Saving New Employee', self.employee);
self.createEmployee();
}else{
console.log('Updating employee with id ', self.employee.employeeId);
self.updateEmployee();
console.log('Employee updated with id ', self.employee.employeeId);
}
self.reset();
};

self.edit = function(employeeId){
console.log('id to be edited', employeeId);
for(var i = 0; i < self.employees.length; i++){
if(self.employees[i].employeeId === employeeId) {
self.employee = angular.copy(self.employees[i]);
break;
}
}
};

self.remove = function(employeeId){
console.log('id to be deleted', employeeId);
if(self.employee.employeeId === employeeId) {//If it is the one shown on screen, reset screen
self.reset();
}
self.deleteEmployee(employeeId);
};


self.reset = function(){
self.employee= new Employee();
$scope.myForm.$setPristine(); //reset Form
};

}]);

最佳答案

您的问题可能是在删除员工之前调用 Employee.get({employeeId:identity}, ...) 时出现的。这将在删除之前加载员工,并将在 'http://localhost:8080/SpringRestExample/employee/:id' 上执行 GET 请求。

为了使此查询正常工作,您需要提供 id,但您尚未提供该信息,因此可能会省略 URL 的该部分。您提供了 employeeId,它仅用于将 id 参数映射到 Employee 对象。尝试用 {id:identity} 替换上面的查询。

关于javascript - Angularjs CRUD 删除问题(在 DELETE 方法中调用 GET),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39991290/

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