【问题标题】:Angularjs- Why my DELETE request is going as Options?Angularjs-为什么我的 DELETE 请求作为选项进行?
【发布时间】: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


    【解决方案1】:

    您正在执行跨域请求,大多数浏览器在实际请求之前发送一个 OPTION 请求以检查 Access-Control-Allow-Origin。如果当前域是允许的,则执行请求。

    【讨论】:

    • 我已经阅读了很多内容并尝试了不同的解决方案,但没有任何进展。我该怎么办?
    • 在您的服务器上处理 OPTION 以返回正确的 Access-Control-Allow-Origin
    【解决方案2】:

    这是服务器端问题。为了安全和节省服务器负载,Web 服务器默认不允许跨源请求。

    由于您在本地主机上工作,因此不允许。它应该可以在实时网站上正常工作。但是,如果您想允许跨源,则必须根据您使用的内容在服务器上的 NGINX/Apache .conf 文件中进行更改。

    例如: 对于 Nginx,您需要在请求中添加一些标头

            add_header 'Access-Control-Allow-Origin' "$http_origin";
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Mx-ReqToken,X-Requested-With';
    

    【讨论】:

    • 感谢您回答确实是服务器端问题。
    【解决方案3】:

    这是一个服务器端问题这个文档解决了我的问题https://gist.github.com/sourcec0de/4237402。

    【讨论】:

      猜你喜欢
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-21
      • 2022-01-25
      • 1970-01-01
      • 2017-05-05
      • 1970-01-01
      • 2021-06-27
      相关资源
      最近更新 更多