Cross-reference request blocked:

I have a mobile app that uses an API to authenticate a user through a login form.

This works fine until today .. and now today when I try to log in, I get the following message in the console log:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://myapp.local/myAppApi/V1/appLogin. 
This can be fixed by moving the resource to the same domain or enabling CORS.

      

Obviously I need to enable CORS to read the post in my myApiController.php. I have the following code in a Yii application that I believe should be doing this:

protected function _renderJSON($status = 200)
{
    $statusCodeMessage = $this->_getStatusCodeMessage($status);
    header("HTTP/1.1 {$status} {$statusCodeMessage}");

    // allow for Cross Origin Resource Sharing
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");
    header("Access-Control-Allow-Headers: Authorization");
    header('Content-type: application/json');
    echo CJSON::encode($this->jsonArray);

    foreach (Yii::app()->log->routes as $route) {
        if ($route instanceof CWebLogRoute) {
            $route->enabled = false; // disable any weblogroutes
        }
    }
    Yii::app()->end();
}

      

Can anyone please help how can I fix this? The app is built with a cordova card and the API it connects to runs through a PHP app built using Yii.

Any advice would be appreciated

- UPDATE - I added the following to my htaccess without joy, however

<ifModule mod_headers.c>
 Header set Access-Control-Allow-Origin: *
 Header set Access-Control-Allow-Headers: Authorization
 Header set Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE
</ifModule>

      

- UPDATE - I came across this link which looks helpful https://gist.github.com/sourcec0de/4237402

+3


source to share


2 answers


Try to add below code to API controller constructor, it works for me.



header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: PUT, GET, POST");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");

      

+4


source


ERROR : Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at the url. This can be fixed by moving the resource to the same domain or enabling CORS.

      

Decision:

i found a solution cross-search query, "fixed"

if you are working on a web project and want to receive data from different sites, Sometimes this error occurs, then you need to use the htaccess file in the root folder

update code



<FilesMatch "\.(php)$">
  <IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
  </IfModule>
</FilesMatch>  

      

if you are a wordpress developer then update the following code:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

<FilesMatch "\.(php)$">
  <IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
  </IfModule>
</FilesMatch>

# END WordPress 

      

thanks :) happy coding:

0


source







All Articles