FernandoPaiva
FernandoPaiva

Reputation: 4460

CORS on localhost to test an app?

I'm trying test an app on browser. The problem is that my webservice is it in localhost also and when I try execute some request to the webservice does returns of CORS exception says same origin policy. Looking for solutions I found a way to disable CORS on Firefox executing this way: about:config -> security.fileuri.strict_origin_policy -> false but doesn't works.

How could I solve this ?

I created the webservice with CakePHP.

CakePHP webservice function

/** do login in app */
public function doLogin(){            
            $this->autoRender = false;
            $this->response->header('Access-Control-Allow-Origin', '*');
            $this->response->type('json');                
            $retorno = array("email"=>"[email protected]", "senha"=>"senha");            
            return json_encode($retorno);
}

Tried add in beforeRender of controller also

public function beforeRender() {
            header("Access-Control-Allow-Origin: *");
            parent::beforeRender();
        }

AngularJS

var app = angular.module("starter");

app.service("UserLoginAPI", function($http, AppConstants, Base64){
    this.doLogin = function(User){
        var _url = "http://localhost/AppPanel/users/doLogin.json";
        var _authdata = Base64.encode(User.email + ':' + User.senha);

        var _headers = {
                'Authorization': 'Basic ' + _authdata,
                'Accept': 'application/json; charset=utf-8',
                'Content-Type': 'application/json; charset=utf-8'               
            };

        return $http({
            method: 'POST',             
            url: _url,              
            headers: _headers               
            });         

    };
});

Upvotes: 2

Views: 1326

Answers (1)

FernandoPaiva
FernandoPaiva

Reputation: 4460

Solved the problem. I added these parameters in Chrome shortcut and now works fine. --args --allow-file-access-from-files --disable-web-security

Upvotes: 1

Related Questions