问题
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