learning-javascript
  • Introduction
  • JavaScript ES5 教學
    • 工具準備
    • 流程控制
    • JavaScript 基本特性, 變數
    • Function及Callback function
    • Array及JSON的操作
    • Object, Scope, this
    • ES5中的自訂物件類型-prototype
    • 進階-再講prototype
    • JavaScript重點整理
    • Closure
    • 多個JS檔, module, timer
    • 進階-module pattern
    • 其他
  • 實際應用
    • Server - HTTP request & response
    • Client - 用Fetch跟Server要資料
  • JavaScript ES6 教學
    • 箭頭函數Arrow Function
Powered by GitBook
On this page
  1. 實際應用

Client - 用Fetch跟Server要資料

PreviousServer - HTTP request & responseNextJavaScript ES6 教學

Last updated 7 years ago

方法種類:

  1. XMLHttpRequest (一般browser內建)

  2. 使用jQuery的$.get(), $.ajax(). (其也是封裝使用了XMLHttpRequest).

  3. 使用Node.js或部份Browser內建的Fetch, e.g.

如果要在全部的Browser上面使用Fetch, 可使用

  1. Bower ), or

  2. CDN [] (), 其介紹: 來安裝.

以下是使用fetch的example, 取得github上任意帳號的資料,

fetch('https://api.github.com/users/任意帳號')
.then(function(res) {
  return res.json();
}).then(function(json){
  console.log(json);
});
https://github.com/bitinn/node-fetch
https://github.com/github/fetch
https://cdnjs.cloudflare.com/ajax/libs/fetch/1.0.0/fetch.min.js
https://cdnjs.cloudflare.com/ajax/libs/fetch/1.0.0/fetch.min.js
https://cdnjs.com/libraries/fetch