CORS 标头 'Access-Control-Allow-OrigCross-Origin 请求已阻止 yii2 [英] CORS header 'Access-Control-Allow-OrigCross-Origin Request Blocked yii2
问题描述
我正在使用 yii2 rest api.我想通过 ajax 获取我的信息,但出现此错误.
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?
推荐答案
您应该阅读以下内容:Cors 过滤器 和 REST Api 和 CORS 过滤器
You should read this : Cors filter and REST Api and CORS filter
跨域资源共享 CORS 是一种机制,允许从资源源自的域之外的另一个域请求网页上的许多资源.特别是,JavaScript 的 AJAX 调用可以使用 XMLHttpRequest 机制.
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.
应在身份验证/授权过滤器之前定义 CORS 过滤器,以确保始终发送 CORS 标头.
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());
}
这篇关于CORS 标头 'Access-Control-Allow-OrigCross-Origin 请求已阻止 yii2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!