1

私は YUI ライブラリを試してみましたが、次のコードに基づいて以下のコードに行き詰まりました: http://yuilibrary.com/yui/docs/jsonp/index.html

私はいくつかの localhost json データを保存しました。これは検証され、ファイル名は data.json で、データは以下のとおりです。

[
   {
      "created_at":"Wed Nov 28 23:13:00 +0000 2012",
      "id":273927502659981312,
      "id_str":"273927502659981312",
      "text":"Get INTO this Season 5 promo for Drag Race - before Viacom sics their copyright-nazis on me.  It's sickening.... http:\/\/t.co\/a6Ld4mKN",
      "source":"\u003ca href=\"http:\/\/www.facebook.com\/twitter\" rel=\"nofollow\"\u003eFacebook\u003c\/a\u003e",
      "truncated":false,
      "in_reply_to_status_id":null,
      "in_reply_to_status_id_str":null,
      "in_reply_to_user_id":null,
      "in_reply_to_user_id_str":null,
      "in_reply_to_screen_name":null,
      "user":{
         "id":8394862,
         "id_str":"8394862",
         "name":"mralexgray",
         "screen_name":"mralexgray",
         "location":"NYC",
         "url":"http:\/\/mrgray.com",
         "description":"Fierceness Incarnate",
         "protected":false,
         "followers_count":129,
         "friends_count":385,
         "listed_count":0,
         "created_at":"Fri Aug 24 01:00:57 +0000 2007",
         "favourites_count":7,
         "utc_offset":-18000,
         "time_zone":"Quito",
         "geo_enabled":true,
         "verified":false,
         "statuses_count":147,
         "lang":"en",
         "contributors_enabled":false,
         "is_translator":false,
         "profile_background_color":"53777A",
         "profile_background_image_url":"http:\/\/a0.twimg.com\/profile_background_images\/88810365\/x86b7f9c12df0fa38ce1a4f29b759706.png",
         "profile_background_image_url_https":"https:\/\/si0.twimg.com\/profile_background_images\/88810365\/x86b7f9c12df0fa38ce1a4f29b759706.png",
         "profile_background_tile":false,
         "profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/2563994027\/2ls5b34rje2nrkqriw2i_normal.png",
         "profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/2563994027\/2ls5b34rje2nrkqriw2i_normal.png",
         "profile_link_color":"C02942",
         "profile_sidebar_border_color":"542437",
         "profile_sidebar_fill_color":"ECD078",
         "profile_text_color":"D95B43",
         "profile_use_background_image":true,
         "default_profile":false,
         "default_profile_image":false,
         "following":null,
         "follow_request_sent":null,
         "notifications":null
      },
      "geo":null,
      "coordinates":null,
      "place":null,
      "contributors":null,
      "retweet_count":0,
      "entities":{
         "hashtags":[

         ],
         "urls":[
            {
               "url":"http:\/\/t.co\/a6Ld4mKN",
               "expanded_url":"http:\/\/fb.me\/1IJWfEnth",
               "display_url":"fb.me\/1IJWfEnth",
               "indices":[
                  113,
                  133
               ]
            }
         ],
         "user_mentions":[

         ]
      },
      "favorited":false,
      "retweeted":false,
      "possibly_sensitive":false
   }
]

次に、次のようなコードを含むこの単純な html ファイルを作成します。

<!DOCTYPE html>
<html>
<head>
<script src="http://yui.yahooapis.com/3.8.0/build/yui/yui-min.js"></script>
<script>
// Create a new YUI instance and populate it with the required modules.
YUI().use('jsonp', 'jsonp-url', 'node' , function (Y) {
    // JSONP is available and ready for use. Add implementation
    // code here.
    var url = "http://localhost/yui/data.json?callback={callback}";

    function handleJSONP(response) {
    // response is a JavaScript object. No parsing necessary
    console.log(response);
    Y.one('#output').setHTML(response.outputHTML);
}
Y.jsonp(url, handleJSONP);
});
</script>
</head>
<body>
<div id="output"></div>
</body>
</html>

コンソールは出力を生成せず、div タグには出力が含まれていません。理由は誰にもわかりますか?

4

1 に答える 1

1

リクエストしている URL は file 用data.jsonであり、サーバーはファイルの内容で応答します。必要なのは、"callback" という名前のクエリ パラメータを読み取り、"{callback}({data.json のコンテンツ});" で応答するサーバー コードによって処理される URL にリクエストを作成することです。

たとえば、「http://localhost/yui/getData.php?callback=foo.bar.baz」へのリクエストを処理する getData.php を、レスポンスとともに記述しますfoo.bar.baz([{"created_at":…&lt;the rest of data.json>…]);

データにアクセスしているページがデータと同じドメインにある場合は、Y.jsonp() ではなく Y.io() + Y.JSON.parse() を使用します。

于 2012-12-16T19:07:39.767 に答える