CORS header 'Access-Control-Allow-OrigCross-Origin Request Blocked yii2

梦想与她 提交于 2021-02-11 06:16:32

问题


I'm using yii2 rest api. I want to get my information by ajax but I'm getting this error.

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://........... (Reason: CORS header 'Access-Control-Allow-Origin' missing).

How can I fix this problem?


回答1:


You should read this : Cors filter and REST Api and CORS filter

Cross-origin resource sharing CORS is a mechanism that allows many resources on a Web page to be requested from another domain outside the domain the resource originated from. In particular, JavaScript's AJAX calls can use the XMLHttpRequest mechanism.

The CORS filter should be defined before Authentication / Authorization filters to make sure the CORS headers will always be sent.

use yii\filters\Cors;
use yii\helpers\ArrayHelper;

public function behaviors()
{
    return ArrayHelper::merge([
        [
            'class' => Cors::className(),
        ],
    ], parent::behaviors());
}


来源:https://stackoverflow.com/questions/39233893/cors-header-access-control-allow-origcross-origin-request-blocked-yii2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!