2012-05-06 31 views

回答

0

看來Tynt API還不支持CORS(跨源資源共享),但沒有實現「訪問控制 - 允許來源:*」響應頭。 因此,您應該嘗試使用像CORS兼容的Yahoo(YQL)這樣的代理服務來讀取此API。

Supose我們有一個全球性的功能:這是從一個jQuery的getJSON調用稱爲readKeywords():

// remember to complete it with your secretkey! 
var api_url = "http://api.tynt.com/publisher/v1/keyword/inbound?site_guid=c4l6yC_Ler4kAMacwqm_6r&api_key=secretkey&hours=24"; 

function readKeywords (data) { 
    var keywords = data.query.results.data; // keywords array 
    var items = []; 

    $.each(keywords, function(index, value) { 
     items.push('<li>' + value.json[0] + ': ' + value.json[1] + '</li>'); 
    }); 

    $('<ul/>', { 
     html: items.join('') 
    }).appendTo('body'); 
} 

$.getJSON("http://query.yahooapis.com/v1/public/yql?format=json&q=select * from json where url='" + encodeURIComponent(api_url) + "' and itemPath='json.data'", readKeywords); 

這個例子讀取雅虎的JSON Tynt的JSON響應,並寫入它作爲document.body的列表其中value.json [0]和value.json [1]是每個入站關鍵字的入站文本和計數器。