和 VUE 前后分离,跨域问题求解 [ 2.0 版本 ]
尝试在Controllers加入behaviors(),未能解决。
public function behaviors()
{
return ArrayHelper::merge([
[
'class' => Cors::className(),
'cors' => [
'Origin' => ['*'],
'Access-Control-Request-Method' => ['*'],
'Access-Control-Allow-Credentials' => true,
],
],
], parent::behaviors());
}
尝试在入口文件加入,未能解决。
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Headers:Authorization');
header("Access-Control-Allow-Methods: GET, POST, DELETE");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Headers: Content-Type, X-Requested-With, Cache-Control,Authorization");
共 6 个回答
-
1.`$response->headers->set('Access-Control-Allow-Origin', '*');
$response->headers->set('Access-Control-Allow-Methods', 'GET,POST,OPTIONS'); $response->headers->set('Access-Control-Request-Headers', 'Origin,X-Requested-With, Content-Type, Accept, Token'); $response->headers->set('Access-Control-Expose-Headers', 'token'); $response->headers->set('Access-Control-Allow-Headers', 'token'); if (Yii::$app->request->getMethod() == 'OPTIONS') { $response->setStatusCode(200); }`
2.有token验证的话
`public function behaviors(){ $behaviors = ArrayHelper::merge(parent::behaviors(), [ 'authenticator' => [ 'class' => QueryParamAuth::className(), 'tokenParam' => 'token', ] ]); $auth = $behaviors['authenticator']; unset($behaviors['authenticator']); $behaviors['corsFilter'] = [ 'class' => \yii\filters\Cors::className(), ]; $behaviors['authenticator'] = $auth; $behaviors['authenticator']['except'] = ['options']; return $behaviors; }`
-
byebye
注册时间:2018-07-18
最后登录:2019-10-18
在线时长:2小时10分
最后登录:2019-10-18
在线时长:2小时10分
- 粉丝0
- 金钱10
- 威望0
- 积分30