Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                html(lang="ja")
  head
    title api取得
  body
    div#content
      h1 外部データ取得(XMLHttpRequest編)
      section#search
        h2 図書・雑誌のタイトル検索
        input#search-word(placeholder="検索したいタイトルを入力してください。")
        button#data-load データ取得
        div#search-result
          h3 検索結果
          ul#data-list

      article#note
        small#note__text このサンプルは、CiNii Articles様のAPIを使用しています。
        a(href="https://support.nii.ac.jp/ja") https://support.nii.ac.jp/ja

              
            
!

CSS

              
                #search
  margin-bottom 30px
#note__text
  display block
              
            
!

JS

              
                const baseUrl = "https://ci.nii.ac.jp/books/opensearch/search";

const searchWord = document.querySelector("#search-word");
const btn = document.querySelector("#data-load");
const dataList = document.querySelector("#data-list");

btn.addEventListener("click", (evt) => {
  const xhr = new XMLHttpRequest();

  if (!searchWord.value) {
    alert("検索ワードが未入力です。");
    return;
  }

  // アクセス元のURLとメソッドを指定
  let urlParameter = {
    format: "json",
    count: 3,
    title: searchWord.value
  };

  let url =
    baseUrl +
    "?" +
    Object.entries(urlParameter)
      .map((e) => {
        let key = e[0];
        let value = encodeURI(e[1]);
        return `${key}=${value}`;
      })
      .join("&");

  xhr.open("GET", url);
  xhr.responseType = "json";

  // ロード成功時の処理
  xhr.onload = function (e) {
    if (this.status != 200) {
      console.log('データ取得失敗')
      return;
    }

    // list初期化
    dataList.innerHTML = "";

    let graph = this.response["@graph"].shift();
    for (const [key, value] of Object.entries(graph.items)) {
      let listItem = document.createElement("li");
      listItem.textContent = value.title;
      dataList.appendChild(listItem);
    }
  };

  xhr.onerror = function(e) {
    console.log('リクエスト失敗');
  }
  
  // apiにリクエストを送信
  xhr.send(null);
});

              
            
!
999px

Console