如何在 AngularJs 中启用 CORS [英] How to enable CORS in AngularJs

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

问题描述

我使用 JavaScript 为 Flickr 照片搜索 API 创建了一个演示.现在我将它转换为 AngularJs.我在互联网上搜索并找到了以下配置.

I have created a demo using JavaScript for Flickr photo search API. Now I am converting it to the AngularJs. I have searched on internet and found below configuration.

配置:

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

服务:

myApp.service('dataService', function($http) {
    delete $http.defaults.headers.common['X-Requested-With'];
    this.flickrPhotoSearch = function() {
        return $http({
            method: 'GET',
            url: 'http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=3f807259749363aaa29c76012fa93945&tags=india&format=json&callback=?',
            dataType: 'jsonp',
            headers: {'Authorization': 'Token token=xxxxYYYYZzzz'}
         });
     }
});

控制器:

myApp.controller('flickrController', function($scope, dataService) {
        $scope.data = null;
        dataService.flickrPhotoSearch().then(function(dataResponse) {
            $scope.data = dataResponse;
            console.log($scope.data);
        });
    });

但是我还是遇到了同样的错误.以下是我尝试过的一些链接:

But still I got the same error. Here are some links I tried:

XMLHttpRequest 无法加载 URL.Access-Control-Allow-Origin 不允许的来源

http://goo.gl/JuS5B1

推荐答案

你没有.您向其发出请求的服务器必须实施 CORS 才能从您的网站访问权限授予 JavaScript.您的 JavaScript 无法授予自己访问其他网站的权限.

You don't. The server you are making the request to has to implement CORS to grant JavaScript from your website access. Your JavaScript can't grant itself permission to access another website.

这篇关于如何在 AngularJs 中启用 CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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