![](/img/trans.png)
[英]React JS - No 'Access-Control-Allow-Origin' header is present on the requested resource. Cross Origin Resource Error
[英]React and Yii2 connection error - No 'Access-Control-Allow-Origin' header is present on the requested resource
我嘗試了在互聯網上找到的數百萬個不同的示例。
但我還是做不到:(
這是我的 REST controller:
<?
namespace app\controllers\admiral_api;
use yii\rest\Controller;
class MainPageController extends Controller {
public function actionIndex() {
return 'test';
}
}
?>
我發現我需要使用 Yii2 在服務器端通過 CORS 過濾器啟用此“訪問控制允許來源”。 無論我嘗試什么,它仍然無法正常工作。
在這種情況下,你有什么適合你的例子嗎? 謝謝!
您需要在 Controller 行為方法中添加 Origin。
public function behaviors() {
return [
'corsFilter' => [
'class' => \yii\filters\Cors::className(),
'cors' => [
// restrict access to
'Origin' => (YII_ENV_PROD) ? [''] : ['http://localhost:3000', 'http://your.store.com'],
// Allow only POST and PUT methods
'Access-Control-Request-Method' => ['GET', 'HEAD', 'POST', 'PUT'],
// Allow only headers 'X-Wsse'
'Access-Control-Request-Headers' => ['X-Wsse', 'Content-Type'],
// Allow credentials (cookies, authorization headers, etc.) to be exposed to the browser
'Access-Control-Allow-Credentials' => true,
// Allow OPTIONS caching
'Access-Control-Max-Age' => 3600,
// Allow the X-Pagination-Current-Page header to be exposed to the browser.
'Access-Control-Expose-Headers' => ['X-Pagination-Current-Page'],
],
],
];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.