我有一个用于用户的CRUD,功能添加和删除工作,但编辑方法不起作用,这是我的代码。此错误
---: Failed to load http://localhost:14539/api/Users: Response for preflight has invalid HTTP status code 405.--------------------------------------------------------------------
使用的代码:
$scope.userEdit = function () {
console.log('edit');
var idsForEdit = [];
angular.forEach($scope.listeOfUsers, function (item,$uibModalInstance, index) {
console.log($scope.listeOfUsers);
if (item.checked) {
console.log(item.checked);
console.log(item);
// idsForEdit.edit(item);
$scope.registration.Email=item.Email;
$scope.registration.Password=item.PasswordHash;
$scope.registration.Users_Role=item.Role;
$scope.registration.Site=item.Site;
$scope.ok = function () {
console.log("ok");
$scope.user=item;
console.log($scope.user);
User.Update({
id: item.Id
}, function (response) {
console.log(response);
console.log("ok");
SweetAlert.swal({
title: "Opération effectuée avec succès!",
text: "Click pour quitter!",
type: "success"
});
$state.go($state.current, $stateParams, {
reload: true,
inherit: false,
notify: true
});
$uibModalInstance.close();
},
function (err) {
});
console.log($scope.user);
};
}
});
//$scope.isEditDirty==true;
};
和此user.controller:
[ResponseType(typeof(void))]
// [HttpPut]
[AcceptVerbs("OPTIONS")]
public IHttpActionResult PutUsers(string id, Users users)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != users.Id)
{
return BadRequest();
}
db.Entry(users).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!UsersExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
您正在获得HTTP 405,因为您的控制器方法设置为接受OPTIONS
请求,而不是PUT
请求。