3

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

Micky
  • 647
  • 1
  • 11
  • 26

1 Answers1

2

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

Community
  • 1
  • 1
Jithu Rajan
  • 452
  • 3
  • 14
  • Understood, nice explanation. But I already did what you suggest.. First point in my tries list:-) – Micky Nov 16 '16 at 13:14
  • Last time it worked for me. Did you close all the chrome instants before running the command? – Jithu Rajan Nov 16 '16 at 13:21
  • Unfortunately yes.. I did a project where locally all the frontend developers were using that parameter..i am really disappointed for loosing all this time. An I cannot change the backend..so need to fix it only client side.. – Micky Nov 16 '16 at 13:29
  • Try this https://productforums.google.com/forum/#!topic/chrome/9nHBcjNW384 – Jithu Rajan Nov 16 '16 at 13:32
  • I confirm that now it is working using that empty parameter.. How did you find that article?:-) Thanks a lot!!! – Micky Nov 16 '16 at 13:59