2016-02-20 56 views
2

我需要通過api獲取我的tumblr帖子。從我的Tumblr API獲取帖子

我設法設置了API密鑰。 我正在使用Angular2,打字稿。

我正在使用jsonp,所以我沒有得到一個交叉來源問題。

這是我目前的嘗試:

var config = { 
    params: { 
     action: "query", 
     prop: "revisions", 
     format: "json", 
     callback: "JSON_CALLBACK" 
    } 
    } 

    var url = "https://api.tumblr.com/v2/blog/thepoolcover.tumblr.com/posts?api_key=lqoK2G4BT9X8xnewyW0l45ky4aTKWTqQ3PzF14gefIglpIRnBz"; 

    this.jsonp.request(url, config).subscribe(response => { 

     console.log(response); 

    }); 

我沒有得到一個打字稿編譯器錯誤,但是,我得到一個瀏覽器異常控制檯錯誤:

enter image description here

+0

如果你把它直接進入瀏覽器的URL工作正常。它返回json中的帖子。但我努力通過上面的代碼來獲取它。不知道我做錯了什麼。我已經嘗試了一個標準的http獲取角2,我得到了一個跨源問題。此外,在過去,我會做一個阿賈克斯調用,但我不知道如何做到這一點在angular2 – AngularM

回答

2

你請使用JSONP_CALLBACK作爲回叫的名稱,而不是JSON_CALLBACK和URLSeachParams用於查詢RY參數:

URLSearchParams search = new URLSearchParams(); 
search.set('action', 'query'); 
search.set('prop', 'revisions'); 
search.set('format', 'json'); 
search.set('callback', 'JSONP_CALLBACK'); 

var config = { 
    search: search 
}; 

var url = "https://api.tumblr.com/v2/blog/thepoolcover.tumblr.com/posts?api_key=lqoK2G4BT9X8xnewyW0l45ky4aTKWTqQ3PzF14gefIglpIRnBz"; 

jsonp.request(url, config).subscribe(response => { 
    console.log(response); 
}); 

下面是相應的plunkr:

+0

我仍然得到相同的錯誤 – AngularM

+0

你也應該使用'URLSearchParams'查詢參數。我更新了我的答案... –