简体   繁体   English

使用AngularJS跨域请求块获取

[英]Get with AngularJS Cross-Origin Request block

It's been a week that I'm stuck on this problem; 我被这个问题困扰了一个星期。 I connect to my server API and retrieve a cookie. 我连接到服务器API并检索Cookie。

I then do a GET to retrieve the JSON returning well Cookie session but I have a problem with Cross Origin. 然后,我做一个GET来检索返回良好的JSON Cookie会话,但是我对Cross Origin有问题。

It is ok on the side of the API because I get a good status 200. 在API方面也可以,因为我的状态为200。

The response : 响应 :

============================================================= ================================================== ===========

RESPONSE : Access-Control-Allow-Cred... true Access-Control-Allow-Head... Accept,Origin,Content-Type,X-Requested-With Access-Control-Allow-Meth... GET, POST, PUT, DELETE Access-Control-Allow-Orig... * Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Connection Keep-Alive Content-Type application/json Date Wed, 06 Aug 2014 18:50:20 GMT Expires Thu, 19 Nov 1981 08:52:00 GMT Keep-Alive timeout=15, max=100 Pragma no-cache Server Apache Transfer-Encoding chunked X-Powered-By PHP/5.3.3 响应:允许访问控制权...真正的允许访问控制头...接受,来源,内容类型,X请求带有允许访问权的方法... GET,POST,放置,删除访问控制允许源... *缓存控制不存储,不缓存,必须重新验证,后检查= 0,预检查= 0连接保持活动内容类型application / json日期星期三,2014年8月6日18:50:20 GMT到期,1981年11月19日星期四08:52:00 GMT Keep-Alive超时= 15,最大= 100 Pragma no-cache服务器Apache传输编码分块X-Powered-By PHP /5.3.3

THE REQUEST: Accept application/json, text/plain, / Accept-Encoding gzip, deflate Accept-Language fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3 Cookie token=97e437a8db5bf96d01e980a3dd81c30cb4bcbf19; 请求:接受application / json,text / plain, /接受编码gzip,压缩接受语言fr,fr-fr; q = 0.8,en-us; q = 0.5,en; q = 0.3 Cookie令牌= 97e437a8db5bf96d01e980a3dd81c30cb4bcbf19; PHPSESSID=kjpqve8tbebhh8u2p6nlu2jki5 Host myapi.com Origin localhost Referer localhost User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0 PHPSESSID = kjpqve8tbebhh8u2p6nlu2jki5主机myapi.com来源localhost引用者localhost用户代理Mozilla / 5.0(Windows NT 6.1; WOW64; rv:31.0)Gecko / 20100101 Firefox / 31.0

Here my request : 这是我的要求:

function loadFeed() {
        $scope.show
      /*  console.log($scope.userid);*/


        $http.get('http://myapi.com/', {withCredentials: true})
            .success(function (data) {

                        $scope.hide();
                        $scope.items = data.data;
                       /* console.log($scope.data); */

                    })
            .error(function(result) {
                        alert("feed ne marche pas");

                    });

    }

I try this in myapp config : 我在myapp config中尝试这个:

.config(['$httpProvider', function($httpProvider) {
     $httpProvider.defaults.headers.common['Content-Type'] = 'application/json; charset=utf-8';
    $httpProvider.defaults.useXDomain = true;
    delete $httpProvider.defaults.headers.common['X-Requested-With'];

 }])   

.config(['$sceDelegateProvider', function($sceDelegateProvider) {
     $sceDelegateProvider.resourceUrlWhitelist(['self', 'http://myapi.com/**', 'http://www.myapi.com/**']);

 }])

I do not understand where is the problem ? 我不明白问题出在哪里?

Finaly, i change my header on my api : Before : header('Access-Control-Allow-Origin: *'); 最后,我在api上更改了标头:之前:header('Access-Control-Allow-Origin:*'); Now : header('Access-Control-Allow-Origin: 192.168.1.87:8100'); 现在:header('Access-Control-Allow-Origin:192.168.1.87:8100');

it works but I don't think it will work when I will compile my mobile application... 它可以工作,但是我认为当我编译移动应用程序时它不会工作...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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