Disable Access-Control-Allow-Origin in Chrome and Firefox

2

I'm developing an app using ionic on the macbook with OS X Yosemite.

When trying to send data via POST or receive via GET to a remote server, the error appears: Chrome:

  

XMLHttpRequest can not load link . In 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' link ' is therefore not allowed access. The response had HTTP status code 405

Firefox:

  

Blocked cross-origin request: Same Origin Policy prevents the remote resource from being read at link . (Reason: CORS request failed).

The original code was:

 function getLeagues(callback){

        $http.get('http://elite-schedule.net/api/leaguedata/2009')
          .success(function(data){
            callback(data);
          });
      }

And I modified it to the following by specifying the header

 $http({
            method: 'GET',
            url: 'http://elite-schedule.net/api/leaguedata/2009', 
            headers: {'Access-Control-Allow-Origin': '*'}
        })
          .success(function(data){
            callback(data);

I already followed the guidance given on the site: opensourehacker

The error persists. How do I disable these restrictions on these browsers by using this operating system?

    
asked by anonymous 11.08.2015 / 04:27

2 answers

3

I had the same problem as you.

The problem is CORS, by default a webserver only accepts requests from the same server, you are probably accessing from outside your backend server (even if the same machine must be in the same apache for example).

You need to enable CORS in the backend, in my case it was a server backend node, follow the example:

res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Request-Width, Content-Type, Accept");
    
11.08.2015 / 23:48
0

It's not just about setting headers on the backend.

The Web-client should define what to accept as well.

In the development environment, you can work around the problem (by disabling CORS checks in the browser) or solve the problem (set the CORS policies on the client and the headers on the server).

See my answer here: link

    
25.11.2015 / 15:08