Home No-Access-Control-Allow-Origin
Reply: 0


user2075 Published in June 21, 2018, 8:41 am

I am getting this error when making a post request:

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.

However a get request to the same endpoint works fine, e.g the following GET request returns data.



 public function behaviors()
    $behaviors = parent::behaviors();
    // remove authentication filter
    // 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;

Any help would be massively appreciated, I am well and truly stuck.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.402802 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO