首页 文章

拒绝从URL执行脚本,因为其MIME类型('application/json')不可执行,并且启用了严格的MIME类型检查

提问于
浏览
1

我正试图在Angular 4 Universal应用程序中从Google检索联系人 .

但是,一旦完成身份验证,我会收到此错误消息:

拒绝执行来自https://www.google.com/m8/feeds/contacts/default/full?access_token=TOKEN&alt=json的脚本,因为其MIME类型('application / json')不可执行,并且严格MIME类型检查已启用 .

目前,所有内容都保存在一个组件中:

import { Component, OnInit } from '@angular/core';
import { Jsonp } from '@angular/http';

@Component({
  selector: 'refer-friend',
  templateUrl: './referFriend.component.html',
})
export class ContactsComponent implements OnInit {
  constructor(private jsonp: Jsonp) { }
  ngOnInit(): void {
    this.auth();
  }
  auth() {
    gapi.auth.authorize({
      'client_id': 'CLIENTID.apps.googleusercontent.com',
      'scope': 'https://www.google.com/m8/feeds'
    }, () => {
      this.fetch(gapi.auth.getToken());  
    });
  }
  fetch(token: any) {
    this.jsonp.get('https://www.google.com/m8/feeds/contacts/default/full?access_token=' + token.access_token + '&alt=json')
     .map(data => {
        return data;
     })
     .subscribe(data => {
         console.log(data);
     });
  }
}

我得到的代码from this sample工作正常,没有发生此错误 . 所以我只能猜测Angular会影响某些东西....

1 回答

  • 2

    Google Contacts API文档包含指向alt标记的工作方式的链接 . 值'json'并不表示它将返回JSONP数据 . Google会返回该MIME类型,因为它将为您提供的数据不可执行 .

    https://developers.google.com/gdata/docs/2.0/reference

    尝试使用 @angular/http 标准get方法而不是 Jsonp .

相关问题