Angular.js地方发展与$ http.get从API [英] Angular.js local development with $http.get from API

查看:126
本文介绍了Angular.js地方发展与$ http.get从API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立Angular.js应用程序,它从一个远程API获取数据的本地开发环境,但我坚持跨域起源策略。该API不提供JSONP或任何东西,除了纯粹的JSON响应。虽然正确地得到我的网站上的作品。是否有任何其他的解决方案比移动开发环境到远程服务器(或者如果它甚至帮助,来移动它)?

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)?

推荐答案

是您的另一种选择是建立CORS在服务器上,并设置您的角度JS应用消费xDomain服务。

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

将服务器设置为启用CORS是很容易使用一些响应头,需要为每个呼叫发送:

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

和作为angularjs你需要设置在app.js的配置部分xDomain电话:

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'];

    })

这篇关于Angular.js地方发展与$ http.get从API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆