# JSON APIs and Ajax

在 vscode 上測試的前置作業

1. 預覽 html
2. 安裝 jQuery 與 bootstrap

```markup
<head>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js">
  </script>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
```

* [VScode 學習筆記 - HackMD](https://hackmd.io/MwYwnApsDsBMYFoCsAOAZgFgRtYCGCewSAjAmLAGwSx7gAMaAJtEA===)
* [Bootstrap 环境安装 | 菜鸟教程](http://www.runoob.com/bootstrap/bootstrap-environment-setup.html)
* [jQuery 安装](http://www.w3school.com.cn/jquery/jquery_install.asp)

## 1-Trigger Click Events with jQuery

在 `$(document).ready(function(){}` 裡新增一個 click 事件(暫時裝飾用)

![](http://i.imgur.com/CxI6759.png)

```markup
<script>
  $(document).ready(function() {
    // Only change code below this line.
    $("#getMessage").on("click", function(){

    });

    // Only change code above this line.
  });
</script>


<div class="container-fluid">
  <div class = "row text-center">
    <h2>Cat Photo Finder</h2>
  </div>
  <div class = "row text-center">
    <div class = "col-xs-12 well message">
      The message will go here
    </div>
  </div>
  <div class = "row text-center">
    <div class = "col-xs-12">
      <button id = "getMessage" class = "btn btn-primary">
        Get Message
      </button>
    </div>
  </div>
</div>
```

## 2-Change Text with Click Events

按下按鈕後，文字會改變

![](http://i.imgur.com/CxI6759.png)

```javascript
<script>
  $(document).ready(function() {
    $("#getMessage").on("click", function(){
      // Only change code below this line.
      $(".message").html("Here is the message");
      // Only change code above this line.
    });
  });
</script>


<div class="container-fluid">
  <div class = "row text-center">
    <h2>Cat Photo Finder</h2>
  </div>
  <div class = "row text-center">
    <div class = "col-xs-12 well message">
      The message will go here
    </div>
  </div>
  <div class = "row text-center">
    <div class = "col-xs-12">
      <button id = "getMessage" class = "btn btn-primary">
        Get Message
      </button>
    </div>
  </div>
</div>
```

## 3-Get JSON with the jQuery getJSON Method

* [cat.json 的網址](https://www.freecodecamp.com/json/cats.json)
* [jQuery ajax - getJSON() 方法](http://www.w3school.com.cn/jquery/ajax_getjson.asp)
* [Node.js 入門, #10：認識 JSON Stringify](http://www.jollen.org/blog/2014/07/nodejs-json-stringify.html)
* 想在 vscode 上複製，結果失敗了。

```markup
<script>
  $(document).ready(function() {

    $("#getMessage").on("click", function(){
      // Only change code below this line.
      $.getJSON("/json/cats.json", function(json) {
        $(".message").html(JSON.stringify(json));
      });

      // Only change code above this line.
    });

  });
</script>

<div class="container-fluid">
  <div class = "row text-center">
    <h2>Cat Photo Finder</h2>
  </div>
  <div class = "row text-center">
    <div class = "col-xs-12 well message">
      The message will go here
    </div>
  </div>
  <div class = "row text-center">
    <div class = "col-xs-12">
      <button id = "getMessage" class = "btn btn-primary">
        Get Message
      </button>
    </div>
  </div>
</div>
```

## 4-Convert JSON Data to HTML

![Imgur](http://i.imgur.com/GLbYJKC.png)

* [javascript - How to parse JSON using Node.js? - Stack Overflow](https://stackoverflow.com/questions/5726729/how-to-parse-json-using-node-js)

```markup
<script>
  $(document).ready(function() {

    $("#getMessage").on("click", function() {
      $.getJSON("/json/cats.json", function(json) {

        var html = "";
        // Only change code below this line.
        json.forEach(function(val) {

          var keys = Object.keys(val);
          html += "<div class = 'cat'>";

          keys.forEach(function(key) {
            html += "<strong>" + key + "</strong>: " 
              + val[key] + "<br>";
          });

          html += "</div><br>";

        });

        // Only change code above this line.

        $(".message").html(html);

      });
    });
  });
</script>

<div class="container-fluid">
  <div class = "row text-center">
    <h2>Cat Photo Finder</h2>
  </div>
  <div class = "row text-center">
    <div class = "col-xs-12 well message">
      The message will go here
   </div>
  </div>
  <div class = "row text-center">
    <div class = "col-xs-12">
      <button id = "getMessage" class = "btn btn-primary">
        Get Message
      </button>
    </div>
  </div>
</div>
```

## 5-Render Images from Data Sources

![Imgur](http://i.imgur.com/Z9f17ss.png)

```markup
<script>
  $(document).ready(function() {

    $("#getMessage").on("click", function() {
      $.getJSON("/json/cats.json", function(json) {

        var html = "";

        json.forEach(function(val) {

          html += "<div class = 'cat'>";

          // Only change code below this line.

          html += "<img src = '" + val.imageLink + "' " 
            + "alt='" + val.altText + "'>";


          // Only change code above this line.

          html += "</div>";

        });

        $(".message").html(html);

      });
    });
  });
</script>

<div class="container-fluid">
  <div class = "row text-center">
    <h2>Cat Photo Finder</h2>
  </div>
  <div class = "row text-center">
    <div class = "col-xs-12 well message">
      The message will go here
    </div>
  </div>
  <div class = "row text-center">
    <div class = "col-xs-12">
      <button id = "getMessage" class = "btn btn-primary">
        Get Message
      </button>
    </div>
  </div>
</div>
```

## 6-Prefilter JSON

```javascript
<script>
  $(document).ready(function() {

    $("#getMessage").on("click", function() {
      $.getJSON("/json/cats.json", function(json) {

        var html = "";

        // Only change code below this line.        
        json = json.filter(function(val){
          return (val.id !== 1);
        })

        // Only change code above this line.

        json.forEach(function(val) {
          html += "<div class = 'cat'>"
          html += "<img src = '" + val.imageLink + "' " 
          + "alt='" + val.altText + "'>"
          html += "</div>"
        });

        $(".message").html(html);

      });
    });
  });
</script>

<div class="container-fluid">
  <div class = "row text-center">
    <h2>Cat Photo Finder</h2>
  </div>
  <div class = "row text-center">
    <div class = "col-xs-12 well message">
      The message will go here
    </div>
  </div>
  <div class = "row text-center">
    <div class = "col-xs-12">
      <button id = "getMessage" class = "btn btn-primary">
        Get Message
      </button>
    </div>
  </div>
</div>
```

## 7-Get Geolocation Data

* [小狐狸事務所: HTML5 Geolocation API (地理位置) 測試](http://yhhuang1966.blogspot.tw/2013/10/html5-geolocation-api.html)
* [地理位置定位 (Geolocation) - Web APIs | MDN](https://developer.mozilla.org/zh-TW/docs/Web/API/Geolocation/Using_geolocation)
* [地理位置：在地圖上顯示使用者或裝置位置  |  Google Maps JavaScript API  |  Google Developers](https://developers.google.com/maps/documentation/javascript/geolocation?hl=zh-tw)

```markup
<script>
  // Only change code below this line.
if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(function(position) {
    $("#data").html("latitude: " + position.coords.latitude + "<br>longitude: " + position.coords.longitude);
  });
}


  // Only change code above this line.
</script>
<div id = "data">
  <h4>You are here:</h4>

</div>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ayugioh2003.gitbook.io/free-code-camp-practice/front-end-development-certification/json-apis-and-ajax.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
