2017-07-12 296 views
1

我試圖在Angular 4 Universal應用程序中從Google中檢索聯繫人。拒絕從URL執行腳本,因爲它的MIME類型('application/json')不可執行,並且啓用嚴格的MIME類型檢查

但我收到此錯誤信息一旦認證已經完成:

拒絕從 https://www.google.com/m8/feeds/contacts/default/full?access_token=TOKEN&alt=json 執行腳本,因爲它的MIME類型(「應用/ 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正在實施一些東西......

+0

你確定這不是重複嗎? https://stackoverflow.com/questions/36289495/how-to-make-a-simple-jsonp-asynchronous-request-in-angular-2 – JGFMK

+1

你也不會在地圖中返回數據。它通常用於執行諸如將https://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html轉換爲json有效內容的響應 - 響應包含HTTP頭等,以便映射(響應:響應=>響應。 JSON())。你也應該把它重構成一個服務,並把它作爲一個Observable返回。理想的接口或領域模型類。我看到您正在訂閱Feed。然後,組件應該使用@ Injectable注入@Injectable作爲組件構造函數中的私有參數,並將其連接到您的app-module.ts – JGFMK

+0

的@ ngModule providers:[],然後該組件變爲... this.service.subscribe (data => this.data = data); – JGFMK

回答

2

Google Contacts API文檔有一個指向alt標籤如何工作的鏈接。值'json'並不表示它會返回JSONP數據。 Google會返回該MIME類型,因爲它會給你的數據不可執行。

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

嘗試使用@angular/http標準get方法,而不是Jsonp

+0

你是對的,謝謝你!我想我對基於它的例子感到困惑,因爲那是使用JSONP進行AJAX調用。 –

相關問題