我在发布帖子请求时收到此错误:
XMLHttpRequest cannot load http://api.dev/v1/call-rates/base/search. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access. The response had HTTP status code 404.
然而,对同一端点的get请求工作正常,例如以下GET
请求返回数据。
`http://api.dev/v1/call-rates/base`
科尔斯:
public function behaviors()
{
$behaviors = parent::behaviors();
// remove authentication filter
unset($behaviors['authenticator']);
// add CORS filter
$behaviors['corsFilter'] = [
'class' => Cors::className(),
'cors' => [
// restrict access to domains:
'Origin' => ['*'],
'Access-Control-Allow-Methods' => ['POST', 'PUT', 'OPTIONS', 'PATCH', 'GET', 'HEAD'],
// Allow only POST and PUT methods
'Access-Control-Request-Headers' => ['Authorization', 'Access-Control-Allow-Headers', 'Content-Type', 'Access-Control-Allow-Methods', 'Origin'],
// Allow the X-Pagination-Current-Page header to be exposed to the browser.
'Access-Control-Expose-Headers' => ['X-Pagination-Page-Count','X-Pagination-Total-Count', 'Origin'],
],
];
// re-add authentication filter
$behaviors['authenticator'] = [
'class' => HttpBearerAuth::className(),
];
// avoid authentication on CORS-pre-flight requests (HTTP OPTIONS method)
$behaviors['authenticator']['except'] = ['options'];
$behaviors['contentNegotiator'] = [
'class' => ContentNegotiator::className(),
'formats' => [
'application/json' => Response::FORMAT_JSON,
],
];
return $behaviors;
}
任何帮助都会受到大力赞赏,我很好并且真的被卡住了。
答案 0 :(得分:0)
我认为你的api通话DayArchiveView
通常与之相关
一个GET请求,而不是POST,但是,在你的web.php中你可以试试这样的
'urlManager'=>[
'class'=> 'yii\web\UrlManager',
'showScriptName'=>false,
'enablePrettyUrl'=>true,
'rules' => [
['class' => 'yii\rest\UrlRule',
'controller' => 'your-controller',
'extraPatterns' => [
'POST your action' => 'your-action'
],
],
['class' => 'yii\rest\UrlRule',
'controller' => 'rest',
],
],
],