【发布时间】:2017-02-27 07:19:57
【问题描述】:
它工作得很好,但有一段时间我已经停止了这个项目。我正在向我的服务器发送一个删除请求,它给了我这个Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://abc.dev/users/users/18. (Reason: CORS preflight channel did not succeed).(unknown)
API 是用 Yii 框架编写的。我正在为 firefox 使用 CORS 扩展,我的 GET, POST 方法工作得很好,但我的 DELETE 方法似乎卡住了。
我的控制器
$scope.delete = function (id)
{
if (confirm("Are you sure you want to delete the user") === true) {
$http({
method: 'DELETE',
url: 'http://abc.dev/users/users/' + id,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function (data) {
$scope.singleuser = data;
console.log("function single user is processed");
$scope.user(); //call the function to reload
})
.error(function (data) {
console.log('error');
});
}
我的后端API删除功能
public function actionDelete() {
switch ($_GET['model']) {
// Load the respective model
case 'users':
$model = User::model()->findByPk($_GET['id']);
break;
default:
$this->_sendResponse(501, sprintf('Error: Mode <b>delete</b> is not implemented for model <b>%s</b>', $_GET['model']));
Yii::app()->end();
}
// Was a model found? If not, raise an error
if ($model === null)
$this->_sendResponse(400, sprintf("Error: Didn't find any model <b>%s</b> with ID <b>%s</b>.", $_GET['model'], $_GET['id']));
// Delete the model
$num = $model->delete();
if ($num > 0)
$this->_sendResponse(200, $num); //this is the only way to work with backbone
else
$this->_sendResponse(500, sprintf("Error: Couldn't delete model <b>%s</b> with ID <b>%s</b>.", $_GET['model'], $_GET['id']));
}
}
【问题讨论】:
标签: angularjs yii cors options