如何在 angularjs 中解决 CORS 即同源策略 [英] How to resolve CORS ie same origin policy in angularjs

查看:28
本文介绍了如何在 angularjs 中解决 CORS 即同源策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 angularjs 中开发一个移动应用程序,我必须在其中调用 Web 服务.但是,当我使用 $http.get 进行调用时,它给出了以下错误.

I am developing a mobile application in angularjs where I have to make call to web service. But, when I am making the call with $http.get it is giving the following error.

XMLHttpRequest cannot load http://example.com/First_Step.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8020' is therefore not allowed access. 

我的功能如下:

$scope.firstStepClick = function() {
    appSvc.selectedItem = "firstStep";
    $rootScope.go('parent/firststep', 'slideLeft');
    delete $http.defaults.headers.common['X-Requested-With'];            
    $http.get("http://example.com/First_Step.json").success(function(data) {
        $scope.firstStepData = data;
    }).error(function() {
        alert("an unexpected error occured");
    });
};

所以,请帮我解决这个 CORS 问题.我正在使用 html5、css3 和 angularjs 进行此移动应用开发.

So, please help me to solve this CORS problem. I am using html5, css3 and angularjs for this mobile app development.

提前致谢

推荐答案

您无需在 Angular 方面做任何事情.

You don't have to do anything in the Angular side.

服务器端负责发送权限头以启用 CORS

The server side is the one responsible to send the rights headers to enable CORS

看看这个:

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

这篇关于如何在 angularjs 中解决 CORS 即同源策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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