Micky Micky - 24 days ago 6
AngularJS Question

Local development not possible with error: No 'Access-Control-Allow-Origin' header is present on the requested resource

I am pretty new in angularjs, and I am developing my first app. I prepared a backend Restful service in another system, which cannot be touched, and I developed my service. This the code:

var MainService = angular.module('MainService', [])
MainService.factory('MainData', ['$http', function ($http) {
var urlBase = 'http://demoint:1234/rest.oms/basvc/barest';
var MainData = {};
MainData.getData = function () {
return $http.get(urlBase + '/0/usecases?generation=true&UseCase=0.0.3550d.a6000015');
};
console.log(MainData);
return MainData;
}]);


But then I get this error on my browser:
XMLHttpRequest cannot load http://demoint:1234/rest.oms/basvc/barest/0/usecases?generation=true&UseCase=0.0.3550d.a6000015. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access.
enter image description here

I tried to bypass the problem in these ways but without luck:


  1. adding this option to Chrome (startup parameter):
    --disable-web-security

  2. Adding this extension https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en

  3. adding the config code below on my main app:

    .config(['$httpProvider', function ($httpProvider) {
    $httpProvider.defaults.useXDomain = true;
    $httpProvider.defaults.withCredentials = true;
    delete $httpProvider.defaults.headers.common["X-Requested-With"];
    $httpProvider.defaults.headers.common["Accept"] = "application/json";
    $httpProvider.defaults.headers.common["Content-Type"] = "application/json";
    }
    ]);



Any idea about how to solve?

enter image description here

Thanks in advance!
Fabio

Answer

First of all JavaScript can't grant itself permission to access another website. means using only JavaScript you cant fix this issue.You have to enable CORS in back end server. If you just want run this only on your browser you can disable web-security in chrome. For this close all the instance of chrome and run chrome.exe --disable-web-security.

For Chrome 49 plus check the link- Chrome 49 plus --disable-web-security