2013年1月31日木曜日

開発環境

『初めてのJavaScript 第2版』(シェリー・パワーズ著(Shelley Powers著)、武舎 広幸+武舎 るみ訳、オライリー・ジャパン、2009年、ISBN978-4-87311-425-5) の15章(Ajaxのデータ - XMLかJSONか)練習問第15-1.を解いてみる。

その他参考書籍

15-1.

コード(BBEdit)

$('#pre0').html('');
$('#d0').html('');
$('#pre0').text("通信開始\n");
var result;
$.ajax({
    url: 'http://mkamimura.com/kamimura_blog/learning_javascript/sample1.xml',
      type: 'GET',
      dataType: 'xml',
      complete: function(xhr, textStatus) {
          $('#pre0').append("通信終了\ntextStatus:" + textStatus + "\n");
      },
      success: function(data, textStatus, xhr) {
          $('#pre0').append("データの取得成功\n");
          var recipe = $(document.createElement('div'));
          recipe.id = "recipe";
          result = data;
          var title = $(document.createElement('p3'));
          title.append($(data).find('recipe > title').text());
          recipe.append(title);
          var ul = $(document.createElement('ul'));
          $(data).find('recipe > ingredient').each(function ( ) {
              var li = $(document.createElement('li'));
              li.append($(this).text());
              ul.append(li);
          });
          recipe.append(ul);
          var p = $(document.createElement('p'));
          p.append($(data).find('recipe > instruction').text());
          recipe.append(p);
          $('#d0').append(recipe);
      },
      error: function(xhr, textStatus, errorThrown) {
        $('#pre0').append("データの取得失敗\n" +
        "textStatus: " + textStatus + ", errorThrown: " + errorThrown + "\n");
      }
});


0 コメント:

コメントを投稿