采用了棱角分明$ http服务和Coinbase API MIME类型错误检查 [英] MIME type checking error using Angular $http service and Coinbase API

查看:479
本文介绍了采用了棱角分明$ http服务和Coinbase API MIME类型错误检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建使用AngularJS一个简单的应用程序,显示比特币对Coinbase使用的 Coinbase API

I'm creating a simple app using AngularJS that displays the current spot rate (price) of Bitcoin on Coinbase using the Coinbase API.

该应用程序按预期工作在Chrome,Safari浏览器,火狐,&安培;歌剧,但在Chrome Canary版和放大器; IE我收到以下错误:

The app works as expected in Chrome, Safari, Firefox, & Opera, however in Chrome Canary & IE I receive the following error:

拒绝从执行脚本
   https://coinbase.com/api/v1/prices/ spot_rate?回调= angular.callbacks._0
  ,因为它的MIME类型(应用/ JSON')不是可执行文件,
  严格的MIME类型检查功能。

我熟悉AngularJS和我使用$ http服务来构建其他应用程序访问供应商API和我没有经历过这个问题。

I'm familiar with AngularJS and I've used $http service to build other apps accessing vendor API's and I've not experienced this issue.

在code下面应该通过Coinbase API的即期汇率,将数据传递到范围为$ http服务回调的一部分,并刷新通过每隔60秒后续调用存储在范围内的值。

The code below should get the spot rate via the Coinbase API, pass the data to the scope as part of the $http service callback, and refresh the value stored in the scope by making subsequent calls every 60 seconds.

angular.module('CoinbaseApp').controller('MainCtrl', function ($scope, $http, $interval) {

    $scope.getPrice = function(){
        $http.jsonp('https://coinbase.com/api/v1/prices/spot_rate?callback=JSON_CALLBACK').success(function(data){
            $scope.data = data;
        });
    };

    $scope.getPrice();

    $interval($scope.getPrice, 60000);
});

我的问题:是严格的MIME类型检查问题与Coinbase是如何服务于JSON的问题吗?或者是有一个问题AngularJS $ http服务和/或如何,我请求数据?

My question: is the strict MIME type checking issue a problem with how Coinbase is serving the json? Or is it an issue with AngularJS $http service and/or how I'm requesting the data?

推荐答案

在呼唤不适当的CORS头响应服务的的不直接支持JSONP,你可以安装一个HTTP请求拦截器重写请求作为GET https://jsonp.afeld.me/ ,移动原始网址进入config.params(与回调一起)。然后定义responseTransform简单地提取并返回嵌入式JSON:

When calling out to a service that doesn't respond with appropriate CORS headers and does not directly support JSONP, you can install an http request interceptor to rewrite the request as a GET https://jsonp.afeld.me/, moving the original URL into the config.params (along with a callback). Then define responseTransform to simply extract and return the embedded JSON:

var app = angular.module('jsonp-proxy-request-interceptor', []);
app.service('jsonpProxyRequestInterceptor',
    function JsonpProxyRequestInterceptor($log) {
  var callbackRx = /callback\((.+)\);/gm;
  this.request = function(config) {
    if (config.url === 'https://api.domain.com/' && config.method === 'GET') {
      var apiUrl = config.url;
      config.url = 'https://jsonp.afeld.me/';
      config.params = angular.extend({}, config.params || {}, {
        url: apiUrl,
        callback: 'callback'
      });
      config.transformResponse.unshift(function(data, headers) {
        var matched = callbackRx.exec(data);
        return matched ? matched[1] : data;
      });
    }
    return config;
  };
});
app.config(['$httpProvider', function configHttp($httpProvider) {
  $httpProvider.interceptors.push('jsonpProxyRequestInterceptor');
}]);

您还可以从 https://gist.github.com/mzipay/叉这个例子的要点69b8e12ad300ecaa467a

这篇关于采用了棱角分明$ http服务和Coinbase API MIME类型错误检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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