简体   繁体   中英

Angular.js local development with $http.get from API

I am trying to set up local development environment of Angular.js app which gets data from a remote API, but I am stuck with cross-domain origin policy. The API is not serving JSONP or anything except pure JSON response. Although, GET works correctly on my website. Is there any other solution than moving dev environment to the remote server (or if it even help, to move it)?

Yes your other option is to setup CORS on the server and set your angular js app to consume xDomain services.

Setting up a server to enable CORS is easy using few response headers which needs to be sent for each call:

You can find out about enabling them based on your server technology here:

http://enable-cors.org/server.html

and as for angularjs you need to setup xDomain calls in the config section of app.js:

.config(function ($stateProvider, $urlRouterProvider, $httpProvider) {

        $httpProvider.defaults.useXDomain = true;

        delete $httpProvider.defaults.headers.common['X-Requested-With'];

    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM