0

I have to work with the API rest but i have this error every time whenever do?

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:8100' is therefore not allowed access.

$scope.submit = function(login, password){
url = 'http://x.x.x.x:x/x/x/x/x/'+login+'?password='+password;
console.log(url);
instanceId = '1';
tokenId = '';
userId = '';
$http.get(url, {
      headers: {
        'Accept': 'application/json',
        'Context': 'instance='+tokenId+';token='+tokenId+';user='+userId
      }
  })
  .success(function(data){
    $scope.firstName = data.user.firstName;
    $scope.lastName = data.user.lastName;
    $scope.profession = data.user.mission;

    userId = data.user.id;*/
    localStorage.setItem("userId", JSON.stringify(userId));
    $location.path('app/home/');
  })
  .error(function(data, status){
    console.log(data + "-" + status);
    alert(status);
});
KLOREL01
  • 3
  • 4
  • 3
    Possible duplicate http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource – Gabriel Câmara May 02 '16 at 16:11
  • http://stackoverflow.com/questions/36002979/xmlhttprequest-cannot-load-and-response-for-preflight-has-invalid-http-status-co – Mohan Gopi May 03 '16 at 12:10

1 Answers1

0

CORS is only an issue when running your app in development mode with ionic serve or ionic run/emulate -l, and not when running as a mobile app packaged with Cordova, a simple option is to just disable CORS altogether for local development. If you use Chrome for debugging, there's a plugin called Allow-Control-Allow-Origin: * that lets you disable CORS.
Or if you want to solve this issue, there has two ways:

  • The first, and easier, solution is to just allow all origins from your API endpoint. The disadvantage is that we can not always control the endpoint we are accessing.
  • Fortunately, we have second way: Ionic CLI proxy server, ionic has supplied an awesome blog about this, please refer to http://blog.ionic.io/handling-cors-issues-in-ionic/.

Hope this will help, regard.

liuwenzhuang
  • 946
  • 7
  • 14