generator-gulp-angular icon indicating copy to clipboard operation
generator-gulp-angular copied to clipboard

Cross-Origin Request Blocked

Open leviathanbeak opened this issue 8 years ago • 1 comments

[This is not an issue with generator, rather with me using it :) but I can't find any help so I'm turning to you guys/gals]

So, I've created an Angular service that gets some data.json from an other API, (using $http.get) and when I use it in my controller and try to send it to HTML I get this error: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://food2fork.com/api/search....

I've researched already, I tried setting my server.middleware (in gulp/server.js) to middleware:

function (req, res, next) { res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader("Access-Control-Allow-Headers", "Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With"); res.setHeader("Access-Control-Allow-Methods", "GET, PUT, POST"); next(); },

But that didn't help, I know there something about proxies but I don't know what to do... Can someone tell me where and what I need to change to make the simple $http.get method to work :)

leviathanbeak avatar Apr 28 '16 13:04 leviathanbeak

You have to set useXDomain = true and delete X-Requested-With in your config() block.

.config(function ($httpProvider) {
    $httpProvider.defaults.useXDomain = true;
    delete $httpProvider.defaults.headers.common['X-Requested-With'];
  }
})

If you use the standard template this should be in index.config.js. For test-case this solution is OK, but in production there is a reason why the browsers implemented CORS blocking.

piu130 avatar Apr 29 '16 07:04 piu130