9

Tumblrアカウントを設定し、それを認証するためにアプリケーションを登録しました。

Tumblrのドキュメント:http ://www.tumblr.com/docs/en/api/v2

APIがJSONを次のように出力することを理解しています。

{
   "meta": {
      "status": 200,
      "msg": "OK"
   },
   "response": {
      "blog": {
         "title": "David's Log",
         "posts": 3456,
         "name": "david",
         "url": "http:\/\/david.tumblr.com\/",
         "updated": 1308953007,
         "description": "<p><strong>Mr. Karp<\/strong> is tall and skinny, with
            unflinching blue eyes a mop of brown hair.\r\n
         "ask": true,
         "ask_anon": false,
         "likes": 12345
      }
   }
}

それは問題ありませんが、ドキュメントはそこで終わります。この情報を取得して自分のサイトに表示する方法がわかりません。

私はあなたがそれを得る方法は次のようなものになるだろうと思いました:

$.ajax({
    url: "http://api.tumblr.com/v2/blog/myblog.tumblr.com/info?api-key=myapikey",
    dataType: 'jsonp',
    success: function(results){
        console.log(results);
    }
});

しかし、これは何もしません。

誰かが私を助けることができますか?ありがとう

4

3 に答える 3

8

resultsは、JSON 構造を参照するために使用できるオブジェクトになりました。結果オブジェクトを console.log すると、Javascript 開発者コンソールに表示され、オブジェクト ツリーを調べることができます。

応答オブジェクト

したがって、成功のコールバックが応答を受信すると、次のものが利用可能になります。

results.meta.status=> 200

results.meta.msg=>「わかりました」

results.response.title=>「デビッドのログ」

results.response.posts=> 3456

results.response.name=>「デビッド」

results.response.url=> " http://david.tumblr.com/ "

results.response.updated=> 1308953007

results.response.description=> " <p><strong>Mr. Karp</strong>.."

results.response.ask=>真

results.response.ask_anon=>偽

results.response.likes=> 12345


ページへの書き込み

実際にページに書き込まれたものを確認したい場合は、document.write などの DOM を変更する関数を使用するか、Jquery を使用しているため $("#myDivId").html(results. response.title);

これを試して:

  • <div id="myDivId"></div>ページのどこかに追加して、
  • $("#myDivId").html(results.response.title);成功のコールバック関数を追加します

$.ajax({
    url: "http://api.tumblr.com/v2/blog/myblog.tumblr.com/info?api_key=myapikey",
    dataType: 'jsonp',
    success: function(results){
        // Logs to your javascript console.
        console.log(results); 
        // writes the title to a div with the Id "myDivId" (ie. <div id="myDivId"></div>)
        $("#myDivId").html(results.response.title); 
    }
});
于 2013-01-25T02:58:11.967 に答える
1

質問コードでは、リクエストタイプが設定されておらず、tumblrによって拒否されていました。jsonpエラー応答は印刷されていました。以下のコードは、jsonpリクエストを正しく行います。

キーは、タイプとdataTypeを指定することでした。幸運の幸せなハッキング。;-)

$.ajax({
    type:'GET',
    url: "http://api.tumblr.com/v2/blog/jdavidnet.tumblr.com/info",
    dataType:'jsonp',
    data: {
        api_key : "myapikey"
    },
    success:function(response){
        console.log(response, arguments);
    }
 });
于 2013-01-25T03:23:28.800 に答える
0

画面にオブジェクトを書き込む 1 つの方法は、その JSON 表現を含む要素をページに追加することです。

$.ajax({
    url: "http://api.tumblr.com/v2/blog/myblog.tumblr.com/info?api-key=myapikey",
    dataType: 'jsonp',
    success: function(results){
        $("body").append(
            $("<pre/>").text(JSON.stringify(results, null, "    "))
        );
    }
});

ここにデモンストレーションがあります: http://jsfiddle.net/MZR2Z/1/

于 2013-01-25T03:14:56.463 に答える