【问题标题】:How to make cross site HTTP GET JSON request如何进行跨站点 HTTP GET JSON 请求
【发布时间】:2014-11-27 21:35:54
【问题描述】:

我正在使用 AngularJS 进行跨站点 json 调用,如下所示:

$http.get('http://ipinfo.io/json').success(function(response){

上述请求适用于 Chrome 和 Mozilla Forefox,但不适用于 IE 9。IE 9 是我测试过的唯一版本。我假设它也不适用于其他 IE 版本。

谁能告诉我我在这里犯的错误,因为它在 IE 9 中不起作用?

【问题讨论】:

  • 我在 IE8 和 IE9 上都对它进行了测试。您是否在控制台中收到任何错误?
  • @GeekOnGadgets:在控制台上只显示Error: Access is denied
  • 你能解决这个问题吗?还是我应该发表我的答案?
  • @GeekOnGadgets:我还不能解决这个问题。如果您能发布您的答案,我将不胜感激。谢谢。

标签: json angularjs internet-explorer http cross-site


【解决方案1】:

我就是这样做的,这对我来说没有任何错误。希望这可以解决您的问题。

//控制器

(function() {
    var ipInfo = function($scope,IPService){
        IPService.ipServiceProvider()
         .success(function(data){
          console.log(data);
       })
         .error(function(status,error){
           console.log(status);
           console.log(data);
       })
    };
    ipInfo.$inject = ['$scope',IPService];
    angular.module('app').controller('ipInfo',ipInfo);
}());

//服务

 (function() {
    var IPService= function($http) {
        var urlBase = "http://ipinfo.io/json";
        var factory = {};
        factory.ipServiceProvider= function() {
            return $http.get(urlBase);
        };
        return factory;
    };
    IPService.$inject = ['$http'];
    angular.module('app').factory('IPService',IPService);
}());

如果您有任何问题,请告诉我

更新: 如果您看到此屏幕,请单击是。这通常在 IE8 和 IE9 中弹出。

【讨论】:

  • 这正是我正在做的,在 Internet Explorer 上出现 Error: Access is denied 错误。在 chrome 和 Mozilla Firefox 上它工作得很好。以下是我打这个电话的方式。 .factory('ipinfoService', ['$http', function ($http) { var factory = {}; factory.findIPInfo = function() { return $http({url: "http://ipinfo.io/json", method: "GET"}) .then(function(resp) { return resp.data; }) .catch(function(err) { console.log('Error during fetching ipinfo'); }); }; return factory; }])。只是无法解决这里的问题。
  • 我在请求页面时没有收到任何此类消息。我只是在控制台上得到一个Error: Access is denied。必须有一种方法来为 IE 编程,既不显示可怕的消息,也不在控制台上提供Error: Access is denied,并且以某种方式允许跨站点请求正常工作,就像 Chrome 和 Firefox 允许的那样。
  • 有意思,你能把你的IE卸载再重新安装吗?
  • 但我不能向使用该应用程序的用户提出同样的要求。即使我在重新安装 IE 后收到消息,它也不应该向我显示该消息,而只是向我显示结果。应该有针对 IE 的解决方案,否则我对 IE 的做法不正确。
  • 用户是否收到此消息?
猜你喜欢
  • 1970-01-01
  • 2018-06-14
  • 2018-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-01
  • 2020-02-10
相关资源
最近更新 更多