SlideShare uma empresa Scribd logo
1 de 35
AJAX Basic
Ryan@MobileDev.TW
1
簡介
•  Asynchronous JavaScript and XML
•  更新部分網頁內容,卻不用重新載入整個網頁
•  事件觸發
•  建立XMLHttpRequest物件
•  送出請求
Browser
•  處理請求
•  建立回應
•  送出需求資料
Server
•  接收資料
•  處理資料
•  更新畫面
Browser
2
主要功能
•  取得後端資料
•  即時更新部分網頁
•  資料驗證
•  資料過濾
3
基於以下標準
•  XMLHttpRequest object
•  發出請求
•  JavaScript/DOM
•  變動網頁內容
•  CSS
•  樣式變化
•  XML
•  常見資料傳輸格式
4
一個基本的AJAX架構
要資料 - 拿到資料 - 顯示資料
5
按下按鈕取得後端資料
6
1.畫面
先佈局畫面:說明文字、預備顯示區、按鈕
7
1.佈局畫面
8
2.按鈕按下後,觸發loadServerData()
9
3.建立XMLHttpRequest物件
•  確認是否可使用XMLHttpRequest
•  若不行,則發出警告
10
4.設定請求送出請求
•  設定請求方法:open(method,url,async)
•  method:傳輸方式 GET / POST
•  url:要請求的網路資源
•  async:非同步/同步 true / false
•  送出請求方法:send(string)
•  POST才需要傳入字串
11
5.處理取得資料
•  確認拿到資料後,將資料顯示到畫面上,並且隱藏按鈕
12
5.處理取得資料
•  readystatechange
•  當readyState改變時觸發
認識XMLHttpRequest的兩個屬性與一個事件
•  readyState
•  0:尚未初始化
•  1:連線已建立
•  2:請求已接收
•  3:請求處理中
•  4:請求處理完成  回應已準備好
•  status
•  200:OK
•  404:找不到

13
顯示今日笑話
利用AJAX取得需求資料
14
運作架構
顯示今日笑話
01/01
笑話A
01/02
笑話B
01/03
笑話C
01/04
笑話D
01/05
笑話E
01/06
笑話F
01/04
笑話D
15
1.準備數則笑話在Server上
•  檔案名稱命名:
•  年月日.txt
•  例如:20150606.txt
16
2.使用AJAX基本架構,修改畫面
•  一個按鈕
•  按鈕文字:顯示今日笑話
•  一個預備顯示區
•  id:showData
17
3.依據今日日期,產生需求檔名
1.  取得今天日期
2.  產生今日日期字串,並加上.txt
3.  設定請求該項資料
18
Lab:顯示今日笑話
•  修改範例,製作一網頁,點擊按鈕後,可顯示今日笑話
19
XML檔案應用 – 取得活動清單
responseText or responseXML
20
取得Server上的XML資料,顯示在網頁上
21
取得Server上的XML資料,顯示在網頁上
22
responseText or responseXML
•  responseText
•  把拿到的資料當字串來處理
•  使用字串物件的方法
•  responseXML
•  把拿到的資料當XML來處理
•  可使用DOM
23
1.修改資料來源
•  使用原先的AJAX基本架構
•  修改要取得的資料來源檔案
24
2.XML
•  取出資料來源的屬性responseXML
•  分析要處理的XML檔案
•  一個活動:一個Info元件
•  活動名稱:title
•  開始日期:startDate
•  如何取出一個元件中某個屬性的屬性值?
25
3.XML - Table
•  前面加上表格的開始語法
•  table border=1
•  trtd日期/tdtd活動/td/tr
•  中間穿插表格語法
•  後面加上表格的結束語法
•  /table
26
查詢今日天氣
跨網域資料存取:JSONP
27
選擇城市,查詢今日天氣
28
選擇城市,查詢今日天氣
•  資料來源
•  OpenWeatherMap.org
•  擷取方式
•  JSONP – 需要匯入jQuery
29
1.佈局畫面與環境設定
•  匯入jQuery
30
1.佈局畫面與環境設定
•  加入下拉式選單
•  當下拉式選單選定項目時,會觸發方法的執行
31
2.資料擷取與處理
I.選定城市時,將城市id傳入
32
2.資料擷取與處理
II.確認資料是否可成功取得
33
3.資料呈現
•  分析拿到的物件
•  顯示當天日期
•  Date()
•  顯示天氣狀態
•  msg.weather[0].description
34
Lab:加上天氣圖示
http://openweathermap.org/api
35

Mais conteúdo relacionado

Mais procurados

编辑器设计Kissy editor
编辑器设计Kissy editor编辑器设计Kissy editor
编辑器设计Kissy editortaobao.com
 
Template mb-kao
Template mb-kaoTemplate mb-kao
Template mb-kaoxwcoder
 
Blazor 與 Radzen 同行
Blazor 與 Radzen 同行Blazor 與 Radzen 同行
Blazor 與 Radzen 同行Jimmy Ho
 
Backbone.js and MVW 101
Backbone.js and MVW 101Backbone.js and MVW 101
Backbone.js and MVW 101Jollen Chen
 
编辑器设计U editor
编辑器设计U editor编辑器设计U editor
编辑器设计U editortaobao.com
 
Kind editor设计思路
Kind editor设计思路Kind editor设计思路
Kind editor设计思路taobao.com
 
QCon - 一次 Clojure Web 编程实战
QCon - 一次 Clojure Web 编程实战QCon - 一次 Clojure Web 编程实战
QCon - 一次 Clojure Web 编程实战dennis zhuang
 
浅析浏览器解析和渲染
浅析浏览器解析和渲染浅析浏览器解析和渲染
浅析浏览器解析和渲染Ailsa126
 
Browser Object Model
Browser Object ModelBrowser Object Model
Browser Object Modeljay li
 
JQuery 学习
JQuery 学习JQuery 学习
JQuery 学习cssrain
 
Script with engine
Script with engineScript with engine
Script with engineWebrebuild
 
Node getting-started
Node getting-startedNode getting-started
Node getting-startedlylijincheng
 
14 Saving Loading and Application States
14 Saving Loading and Application States14 Saving Loading and Application States
14 Saving Loading and Application StatesTom Fan
 
前端调试工具,编码相关,性能相关
前端调试工具,编码相关,性能相关前端调试工具,编码相关,性能相关
前端调试工具,编码相关,性能相关jay li
 
Android vs e pub
Android vs e pubAndroid vs e pub
Android vs e pub永昇 陳
 
前端跨域总结
前端跨域总结前端跨域总结
前端跨域总结zhangsuoyong
 
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!Will Huang
 

Mais procurados (20)

编辑器设计Kissy editor
编辑器设计Kissy editor编辑器设计Kissy editor
编辑器设计Kissy editor
 
Template mb-kao
Template mb-kaoTemplate mb-kao
Template mb-kao
 
Blazor 與 Radzen 同行
Blazor 與 Radzen 同行Blazor 與 Radzen 同行
Blazor 與 Radzen 同行
 
Backbone.js and MVW 101
Backbone.js and MVW 101Backbone.js and MVW 101
Backbone.js and MVW 101
 
Jsonp
JsonpJsonp
Jsonp
 
编辑器设计U editor
编辑器设计U editor编辑器设计U editor
编辑器设计U editor
 
Kind editor设计思路
Kind editor设计思路Kind editor设计思路
Kind editor设计思路
 
QCon - 一次 Clojure Web 编程实战
QCon - 一次 Clojure Web 编程实战QCon - 一次 Clojure Web 编程实战
QCon - 一次 Clojure Web 编程实战
 
浅析浏览器解析和渲染
浅析浏览器解析和渲染浅析浏览器解析和渲染
浅析浏览器解析和渲染
 
Browser Object Model
Browser Object ModelBrowser Object Model
Browser Object Model
 
Html 5 native drag
Html 5 native dragHtml 5 native drag
Html 5 native drag
 
JQuery 学习
JQuery 学习JQuery 学习
JQuery 学习
 
Script with engine
Script with engineScript with engine
Script with engine
 
Node getting-started
Node getting-startedNode getting-started
Node getting-started
 
Django step0
Django step0Django step0
Django step0
 
14 Saving Loading and Application States
14 Saving Loading and Application States14 Saving Loading and Application States
14 Saving Loading and Application States
 
前端调试工具,编码相关,性能相关
前端调试工具,编码相关,性能相关前端调试工具,编码相关,性能相关
前端调试工具,编码相关,性能相关
 
Android vs e pub
Android vs e pubAndroid vs e pub
Android vs e pub
 
前端跨域总结
前端跨域总结前端跨域总结
前端跨域总结
 
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
 

Destaque

深入淺出RWD自適應網頁設計
深入淺出RWD自適應網頁設計深入淺出RWD自適應網頁設計
深入淺出RWD自適應網頁設計Marie Chang
 
Web前端性能优化 2014
Web前端性能优化 2014Web前端性能优化 2014
Web前端性能优化 2014Yubei Li
 
JavaScript 從零開始
JavaScript 從零開始JavaScript 從零開始
JavaScript 從零開始Adam Hung
 
Sublime text 極速應用教學
Sublime text 極速應用教學Sublime text 極速應用教學
Sublime text 極速應用教學Amos Lee
 
基于原型的JavaScript面向对象编程
基于原型的JavaScript面向对象编程基于原型的JavaScript面向对象编程
基于原型的JavaScript面向对象编程zhangdaiping
 
NodeJS基礎教學&簡介
NodeJS基礎教學&簡介NodeJS基礎教學&簡介
NodeJS基礎教學&簡介GO LL
 
Online Education Resources
Online Education ResourcesOnline Education Resources
Online Education ResourcesRyan Chung
 
Git 實務圖解
Git 實務圖解Git 實務圖解
Git 實務圖解Pokai Chang
 
Full stack-development with node js
Full stack-development with node jsFull stack-development with node js
Full stack-development with node jsXuefeng Zhang
 
阿肯 - UI & UX 從核心資訊與路徑設計談起 (2015/01/27)
阿肯 - UI & UX 從核心資訊與路徑設計談起 (2015/01/27)阿肯 - UI & UX 從核心資訊與路徑設計談起 (2015/01/27)
阿肯 - UI & UX 從核心資訊與路徑設計談起 (2015/01/27)阿肯 KEN studio
 
jQuery 教學 ( 搭配 EZoApp )
jQuery 教學 ( 搭配 EZoApp )jQuery 教學 ( 搭配 EZoApp )
jQuery 教學 ( 搭配 EZoApp )EZoApp
 
第一次用 Vue.js 就愛上 [改]
第一次用 Vue.js 就愛上 [改]第一次用 Vue.js 就愛上 [改]
第一次用 Vue.js 就愛上 [改]Kuro Hsu
 

Destaque (15)

深入淺出RWD自適應網頁設計
深入淺出RWD自適應網頁設計深入淺出RWD自適應網頁設計
深入淺出RWD自適應網頁設計
 
Semantic ui教學
Semantic ui教學Semantic ui教學
Semantic ui教學
 
Web前端性能优化 2014
Web前端性能优化 2014Web前端性能优化 2014
Web前端性能优化 2014
 
JavaScript 從零開始
JavaScript 從零開始JavaScript 從零開始
JavaScript 從零開始
 
Sublime text 極速應用教學
Sublime text 極速應用教學Sublime text 極速應用教學
Sublime text 極速應用教學
 
基于原型的JavaScript面向对象编程
基于原型的JavaScript面向对象编程基于原型的JavaScript面向对象编程
基于原型的JavaScript面向对象编程
 
NodeJS基礎教學&簡介
NodeJS基礎教學&簡介NodeJS基礎教學&簡介
NodeJS基礎教學&簡介
 
響應式網頁教學
響應式網頁教學響應式網頁教學
響應式網頁教學
 
Online Education Resources
Online Education ResourcesOnline Education Resources
Online Education Resources
 
Git 實務圖解
Git 實務圖解Git 實務圖解
Git 實務圖解
 
Full stack-development with node js
Full stack-development with node jsFull stack-development with node js
Full stack-development with node js
 
阿肯 - UI & UX 從核心資訊與路徑設計談起 (2015/01/27)
阿肯 - UI & UX 從核心資訊與路徑設計談起 (2015/01/27)阿肯 - UI & UX 從核心資訊與路徑設計談起 (2015/01/27)
阿肯 - UI & UX 從核心資訊與路徑設計談起 (2015/01/27)
 
Membina 5 ayat
Membina 5 ayatMembina 5 ayat
Membina 5 ayat
 
jQuery 教學 ( 搭配 EZoApp )
jQuery 教學 ( 搭配 EZoApp )jQuery 教學 ( 搭配 EZoApp )
jQuery 教學 ( 搭配 EZoApp )
 
第一次用 Vue.js 就愛上 [改]
第一次用 Vue.js 就愛上 [改]第一次用 Vue.js 就愛上 [改]
第一次用 Vue.js 就愛上 [改]
 

Semelhante a AJAX Basic

Ch03 請求與回應
Ch03 請求與回應Ch03 請求與回應
Ch03 請求與回應Justin Lin
 
John Resig Beijing 2010 (中文版)
John Resig Beijing 2010 (中文版)John Resig Beijing 2010 (中文版)
John Resig Beijing 2010 (中文版)Jia Mi
 
移动搜索在慢网速下的优化
移动搜索在慢网速下的优化移动搜索在慢网速下的优化
移动搜索在慢网速下的优化Charlee Green
 
Servlet & JSP 教學手冊第二版 - 第 3 章:請求與回應
Servlet & JSP 教學手冊第二版 - 第 3 章:請求與回應Servlet & JSP 教學手冊第二版 - 第 3 章:請求與回應
Servlet & JSP 教學手冊第二版 - 第 3 章:請求與回應Justin Lin
 
Real time web实时信息流推送
Real time web实时信息流推送Real time web实时信息流推送
Real time web实时信息流推送yongboy
 
Real-Time Web实时信息流推送
Real-Time Web实时信息流推送Real-Time Web实时信息流推送
Real-Time Web实时信息流推送yongboy
 
Ch03 請求與回應
Ch03 請求與回應Ch03 請求與回應
Ch03 請求與回應Justin Lin
 
2015.07.22 新人報告(3)
2015.07.22 新人報告(3)2015.07.22 新人報告(3)
2015.07.22 新人報告(3)Chih-Wei Chuang
 
12. 網路通訊方案
12. 網路通訊方案12. 網路通訊方案
12. 網路通訊方案Justin Lin
 
2011 JavaTwo JSF 2.0
2011 JavaTwo JSF 2.02011 JavaTwo JSF 2.0
2011 JavaTwo JSF 2.0Anthony Chen
 
运维系统开发与Rails 3页面开发实践
运维系统开发与Rails 3页面开发实践运维系统开发与Rails 3页面开发实践
运维系统开发与Rails 3页面开发实践Li JianYe
 
前端性能测试
前端性能测试前端性能测试
前端性能测试tbmallf2e
 
【第一季•第六期】Ajax & JSONP in Action
【第一季•第六期】Ajax & JSONP in Action【第一季•第六期】Ajax & JSONP in Action
【第一季•第六期】Ajax & JSONP in Actiontbosstraining
 
使用Big pipe提升浏览速度 wk_velocity
使用Big pipe提升浏览速度 wk_velocity使用Big pipe提升浏览速度 wk_velocity
使用Big pipe提升浏览速度 wk_velocitykumawu
 
Dev camps Windows Store App 市集應用程式最佳實踐
Dev camps   Windows Store App 市集應用程式最佳實踐Dev camps   Windows Store App 市集應用程式最佳實踐
Dev camps Windows Store App 市集應用程式最佳實踐Meng-Ru (Raymond) Tsai
 
实时Web的前世今生未来
实时Web的前世今生未来实时Web的前世今生未来
实时Web的前世今生未来RolfZhang
 
11. DOM、事件與樣式
11. DOM、事件與樣式11. DOM、事件與樣式
11. DOM、事件與樣式Justin Lin
 

Semelhante a AJAX Basic (20)

Ch03 請求與回應
Ch03 請求與回應Ch03 請求與回應
Ch03 請求與回應
 
John Resig Beijing 2010 (中文版)
John Resig Beijing 2010 (中文版)John Resig Beijing 2010 (中文版)
John Resig Beijing 2010 (中文版)
 
移动搜索在慢网速下的优化
移动搜索在慢网速下的优化移动搜索在慢网速下的优化
移动搜索在慢网速下的优化
 
Servlet & JSP 教學手冊第二版 - 第 3 章:請求與回應
Servlet & JSP 教學手冊第二版 - 第 3 章:請求與回應Servlet & JSP 教學手冊第二版 - 第 3 章:請求與回應
Servlet & JSP 教學手冊第二版 - 第 3 章:請求與回應
 
Real time web实时信息流推送
Real time web实时信息流推送Real time web实时信息流推送
Real time web实时信息流推送
 
Real-Time Web实时信息流推送
Real-Time Web实时信息流推送Real-Time Web实时信息流推送
Real-Time Web实时信息流推送
 
Ch03 請求與回應
Ch03 請求與回應Ch03 請求與回應
Ch03 請求與回應
 
Berserk js
Berserk jsBerserk js
Berserk js
 
2015.07.22 新人報告(3)
2015.07.22 新人報告(3)2015.07.22 新人報告(3)
2015.07.22 新人報告(3)
 
微博架构Ppt
微博架构Ppt微博架构Ppt
微博架构Ppt
 
12. 網路通訊方案
12. 網路通訊方案12. 網路通訊方案
12. 網路通訊方案
 
2011 JavaTwo JSF 2.0
2011 JavaTwo JSF 2.02011 JavaTwo JSF 2.0
2011 JavaTwo JSF 2.0
 
运维系统开发与Rails 3页面开发实践
运维系统开发与Rails 3页面开发实践运维系统开发与Rails 3页面开发实践
运维系统开发与Rails 3页面开发实践
 
前端性能测试
前端性能测试前端性能测试
前端性能测试
 
Gwt rpc
Gwt rpcGwt rpc
Gwt rpc
 
【第一季•第六期】Ajax & JSONP in Action
【第一季•第六期】Ajax & JSONP in Action【第一季•第六期】Ajax & JSONP in Action
【第一季•第六期】Ajax & JSONP in Action
 
使用Big pipe提升浏览速度 wk_velocity
使用Big pipe提升浏览速度 wk_velocity使用Big pipe提升浏览速度 wk_velocity
使用Big pipe提升浏览速度 wk_velocity
 
Dev camps Windows Store App 市集應用程式最佳實踐
Dev camps   Windows Store App 市集應用程式最佳實踐Dev camps   Windows Store App 市集應用程式最佳實踐
Dev camps Windows Store App 市集應用程式最佳實踐
 
实时Web的前世今生未来
实时Web的前世今生未来实时Web的前世今生未来
实时Web的前世今生未来
 
11. DOM、事件與樣式
11. DOM、事件與樣式11. DOM、事件與樣式
11. DOM、事件與樣式
 

Mais de Ryan Chung

Voice-First Games for Alexa
Voice-First Games for AlexaVoice-First Games for Alexa
Voice-First Games for AlexaRyan Chung
 
AI Service Integration - Education
AI Service Integration - EducationAI Service Integration - Education
AI Service Integration - EducationRyan Chung
 
AI Service Integration
AI Service IntegrationAI Service Integration
AI Service IntegrationRyan Chung
 
AI Adoption in the Enterprise
AI Adoption in the EnterpriseAI Adoption in the Enterprise
AI Adoption in the EnterpriseRyan Chung
 
Intro to Dialogflow Chatbot Development
Intro to Dialogflow Chatbot DevelopmentIntro to Dialogflow Chatbot Development
Intro to Dialogflow Chatbot DevelopmentRyan Chung
 
AI in Classroom
AI in ClassroomAI in Classroom
AI in ClassroomRyan Chung
 
AWS re:Invent 2018 Recap
AWS re:Invent 2018 RecapAWS re:Invent 2018 Recap
AWS re:Invent 2018 RecapRyan Chung
 
Service Integration Workshop
Service Integration WorkshopService Integration Workshop
Service Integration WorkshopRyan Chung
 
Smart Home Intro Lab
Smart Home Intro LabSmart Home Intro Lab
Smart Home Intro LabRyan Chung
 
Introduction to DialogFlow
Introduction to DialogFlow Introduction to DialogFlow
Introduction to DialogFlow Ryan Chung
 
Conversational AI Orientation
Conversational AI OrientationConversational AI Orientation
Conversational AI OrientationRyan Chung
 
Udacity Meet Up - 0413
Udacity Meet Up - 0413Udacity Meet Up - 0413
Udacity Meet Up - 0413Ryan Chung
 
Amazon Alexa Development Part II
Amazon Alexa Development Part IIAmazon Alexa Development Part II
Amazon Alexa Development Part IIRyan Chung
 
Amazon Alexa Development
Amazon Alexa DevelopmentAmazon Alexa Development
Amazon Alexa DevelopmentRyan Chung
 
Microsoft Professional Program - AI
Microsoft Professional Program - AIMicrosoft Professional Program - AI
Microsoft Professional Program - AIRyan Chung
 
AI Service Integration
AI Service IntegrationAI Service Integration
AI Service IntegrationRyan Chung
 
Hour of Code - Amazon Alexa
Hour of Code - Amazon AlexaHour of Code - Amazon Alexa
Hour of Code - Amazon AlexaRyan Chung
 

Mais de Ryan Chung (20)

Voice-First Games for Alexa
Voice-First Games for AlexaVoice-First Games for Alexa
Voice-First Games for Alexa
 
AI Service Integration - Education
AI Service Integration - EducationAI Service Integration - Education
AI Service Integration - Education
 
AI Service Integration
AI Service IntegrationAI Service Integration
AI Service Integration
 
AI Adoption in the Enterprise
AI Adoption in the EnterpriseAI Adoption in the Enterprise
AI Adoption in the Enterprise
 
Intro to Dialogflow Chatbot Development
Intro to Dialogflow Chatbot DevelopmentIntro to Dialogflow Chatbot Development
Intro to Dialogflow Chatbot Development
 
AI in Classroom
AI in ClassroomAI in Classroom
AI in Classroom
 
AWS re:Invent 2018 Recap
AWS re:Invent 2018 RecapAWS re:Invent 2018 Recap
AWS re:Invent 2018 Recap
 
MovieBot
MovieBotMovieBot
MovieBot
 
Service Integration Workshop
Service Integration WorkshopService Integration Workshop
Service Integration Workshop
 
MPP AI
MPP AIMPP AI
MPP AI
 
Smart Home Intro Lab
Smart Home Intro LabSmart Home Intro Lab
Smart Home Intro Lab
 
Introduction to DialogFlow
Introduction to DialogFlow Introduction to DialogFlow
Introduction to DialogFlow
 
Conversational AI Orientation
Conversational AI OrientationConversational AI Orientation
Conversational AI Orientation
 
Udacity Meet Up - 0413
Udacity Meet Up - 0413Udacity Meet Up - 0413
Udacity Meet Up - 0413
 
Amazon Alexa Development Part II
Amazon Alexa Development Part IIAmazon Alexa Development Part II
Amazon Alexa Development Part II
 
Amazon Alexa Development
Amazon Alexa DevelopmentAmazon Alexa Development
Amazon Alexa Development
 
Microsoft Professional Program - AI
Microsoft Professional Program - AIMicrosoft Professional Program - AI
Microsoft Professional Program - AI
 
AI Service Integration
AI Service IntegrationAI Service Integration
AI Service Integration
 
Intro to AI
Intro to AIIntro to AI
Intro to AI
 
Hour of Code - Amazon Alexa
Hour of Code - Amazon AlexaHour of Code - Amazon Alexa
Hour of Code - Amazon Alexa
 

AJAX Basic