首页 > 解决方案 > angularjs 和 CI:预检响应无效(重定向)

问题描述

请帮助我通过这几天的尝试来学习 angularjs 中的基本 crud 操作我刚刚得到了显示表数据的输出(使用 $http.get)接下来我尝试执行删除但我得到了 CORS 错误。是的,但记录被成功删除。

我的代码点火器功能是这样的:

public function delete_user($user_id){ 
header('Content-Type: application/json;charset=UTF-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: DELETE, HEAD, GET, OPTIONS, POST, 
PUT');
header('Access-Control-Allow-Headers: Content-Type, Content-Range, Content- 
Disposition, Content-Description');

$this->users_model->delete_user($user_id);
redirect('Users/index');
}

angularjs中的控制器功能:

$scope.delete = function(id)
{         
$http.delete('http://localhost/**/****/*****/users/delete_user/'+ id)
.then(function(response){
$scope.userList= response.data; 
});
}

标签: angularjscodeigniter

解决方案


因为您已经添加了 OPTIONS

只需添加

if ( "OPTIONS" === $_SERVER['REQUEST_METHOD'] ) {
    die();
}

另请参阅此答案


推荐阅读