JQuery を使用して vine から人気のあるタイムラインを取得して REST 呼び出しを行うことができませんでした。
このコードを使用したとき:
$.ajax({
type: "GET",
crossDomain: true,
dataType: "json",
url: "https://api.vineapp.com/timelines/popular",
success: function(data){
document.getElementById("#site-wrapper").innerText=data;
},
error: function(jqXHR, textStatus, errorThrown){
var json = JSON.stringify(jqXHR, null, 4);
document.getElementById("#site-wrapper").innerText=json;
}
});
次のエラーが表示されます。
XMLHttpRequest cannot load https://api.vineapp.com/timelines/popular. No 'Access-Control-Allow-Origin' header is present on the requested resource.
ちょっとした調査の後、クロスドメインの問題について発見し、長い話を短くするために、コードをこれに変更することになりました。
新しいコード:
$.ajax({
type: "GET",
dataType: "jsonp", //changed data type to jsonp
url: "https://api.vineapp.com/timelines/popular",
success: function(data){
poke = data;
document.getElementById("#site-wrapper").innerText=data;
},
error: function(jqXHR, textStatus, errorThrown){
poke = jqXHR;
var json = JSON.stringify(jqXHR, null, 4);
document.getElementById("#site-wrapper").innerText=json;
}
});
この変更により、クロスドメインの問題を回避できましたが、次のエラーが発生しました。
Uncaught SyntaxError: Unexpected token :
これで、このエラーが発生した理由がわかりました。これは、Vine が JSONP データではなく JSON データを返すという事実によるものです (基本的には JSON ですが、関数などをラップしていると理解しています)。
JSONデータを返しながらクロスドメインの問題を修正する方法はありますか? この呼び出しを行うさまざまなバリエーションを試しましたが、どれも機能しません。事前に感謝します。