AJAX Discussion part 1
Số trang: 6
Loại file: pdf
Dung lượng: 151.61 KB
Lượt xem: 11
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Javascipt CheatsheetAJAX Cheatsheet Khởi tạo đối tượng XMLHttpRequest (XHR) PHP Code: [b]function [/b][b]createXHRobj()[/b] { if (window.XMLHttpRequest) req = new XMLHttpRequest() // browsers besides IE else if (window.ActiveXObject) req = new ActiveXObject("Microsoft.XMLHTTP") // IE else alert ("Could not create XHR obj!") return req }
Nội dung trích xuất từ tài liệu:
AJAX Discussion part 1Javascipt CheatsheetAJAX CheatsheetKhởi tạo đối tượng XMLHttpRequest (XHR)PHP Code:[b]function [/b][b]createXHRobj()[/b] { if (window.XMLHttpRequest) req = new XMLHttpRequest() // browsers besides IE else if (window.ActiveXObject) req = new ActiveXObject(Microsoft.XMLHTTP) // IE else alert (Could not create XHR obj!) return req}Asynchronous GET requestPHP Code:var req=createXHRobj()function request(url) { req.onreadystatechange = callbackFunction req.open(GET, url, true) // 3rd param specifies asynchronous request (browser does not wait/block) req.send()}function callbackFunction() { if (req.readyState == 4) // request completed if (req.status == 200) // HTTP response code (200 == OK) response = req.responseText // and anything else you might need to do else { // error reporting code }}Synchronous GET requestPHP Code:function request(url) { req=createXHRobj() req.open(GET, url, false) req.send(null) alert(req.responseText)}Sending via POST - req.send()PHP Code:function submitform(url) { req=createXHRobj() // DO NOT EVER use the same names for javascript variables and // HTML element ids, they share the same namespace under IE and // may also cause problems in other browsers. namevar=encodeURIComponent(document.getElementById(Sender_Name).value) emailvar=encodeURIComponent(document.getElementById(Sender_Email).value) req.open(POST, url, false) // req.setRequestHeader() must come AFTER req.open() req.setRequestHeader(Content-Type,application/x-www-form-urlencoded) req.send(name=+namevar+&email=+emailvar) alert(req.responseText)}// we do away with HTML form submit to simplify things // long descriptive names that will not be used as // javascript variable names are a good habithere... Wrapper code for Asynchronous requestsPHP Code:var req1=createXHRobj()var req2=createXHRobj()function asynXHR(url, reqobj, action) { reqobj.onreadystatechange=action reqobj.open(GET, url, true) reqobj.send()}function responseIsReady(reqobj) { if (reqobj.readyState==4) if (reqobj.status==200) return true else { alert(ERROR: STATUS RESPONSE +reqobj.status) return true // let the call proceed so we know which // call the status error occurred in } else return false}// each callback function must use its own request objectvar req1, req2function showstock() { if (responseIsReady(req1)) alert(stock price: $+req1.responseText) /* IE cant reuse the XHR object, you are required to clean up and create a new one */ req1=null req1=createXHRobj()}function showtemp() { if (responseIsReady(req2)) alert(Its +req2.responseText+ celsius) /* IE cant reuse the XHR object, you are required to clean up and create a new one */ req2=null req2=createXHRobj()}asynXHR(price.php?stock=GOOG,req1,showstock)asynXHR(temperature.php?city=Singapore,req2,showtemp)One.N(UDS)
Nội dung trích xuất từ tài liệu:
AJAX Discussion part 1Javascipt CheatsheetAJAX CheatsheetKhởi tạo đối tượng XMLHttpRequest (XHR)PHP Code:[b]function [/b][b]createXHRobj()[/b] { if (window.XMLHttpRequest) req = new XMLHttpRequest() // browsers besides IE else if (window.ActiveXObject) req = new ActiveXObject(Microsoft.XMLHTTP) // IE else alert (Could not create XHR obj!) return req}Asynchronous GET requestPHP Code:var req=createXHRobj()function request(url) { req.onreadystatechange = callbackFunction req.open(GET, url, true) // 3rd param specifies asynchronous request (browser does not wait/block) req.send()}function callbackFunction() { if (req.readyState == 4) // request completed if (req.status == 200) // HTTP response code (200 == OK) response = req.responseText // and anything else you might need to do else { // error reporting code }}Synchronous GET requestPHP Code:function request(url) { req=createXHRobj() req.open(GET, url, false) req.send(null) alert(req.responseText)}Sending via POST - req.send()PHP Code:function submitform(url) { req=createXHRobj() // DO NOT EVER use the same names for javascript variables and // HTML element ids, they share the same namespace under IE and // may also cause problems in other browsers. namevar=encodeURIComponent(document.getElementById(Sender_Name).value) emailvar=encodeURIComponent(document.getElementById(Sender_Email).value) req.open(POST, url, false) // req.setRequestHeader() must come AFTER req.open() req.setRequestHeader(Content-Type,application/x-www-form-urlencoded) req.send(name=+namevar+&email=+emailvar) alert(req.responseText)}// we do away with HTML form submit to simplify things // long descriptive names that will not be used as // javascript variable names are a good habithere... Wrapper code for Asynchronous requestsPHP Code:var req1=createXHRobj()var req2=createXHRobj()function asynXHR(url, reqobj, action) { reqobj.onreadystatechange=action reqobj.open(GET, url, true) reqobj.send()}function responseIsReady(reqobj) { if (reqobj.readyState==4) if (reqobj.status==200) return true else { alert(ERROR: STATUS RESPONSE +reqobj.status) return true // let the call proceed so we know which // call the status error occurred in } else return false}// each callback function must use its own request objectvar req1, req2function showstock() { if (responseIsReady(req1)) alert(stock price: $+req1.responseText) /* IE cant reuse the XHR object, you are required to clean up and create a new one */ req1=null req1=createXHRobj()}function showtemp() { if (responseIsReady(req2)) alert(Its +req2.responseText+ celsius) /* IE cant reuse the XHR object, you are required to clean up and create a new one */ req2=null req2=createXHRobj()}asynXHR(price.php?stock=GOOG,req1,showstock)asynXHR(temperature.php?city=Singapore,req2,showtemp)One.N(UDS)
Tìm kiếm theo từ khóa liên quan:
máy tính mạng máy tính internet phần mềm ứng dụng lập trình dữ liệu ajax autoltGợi ý tài liệu liên quan:
-
Giáo án Tin học lớp 9 (Trọn bộ cả năm)
149 trang 246 0 0 -
Ngân hàng câu hỏi trắc nghiệm môn mạng máy tính
99 trang 235 1 0 -
47 trang 235 3 0
-
Đề cương chi tiết học phần Thiết kế và cài đặt mạng
3 trang 229 0 0 -
Giáo trình Hệ thống mạng máy tính CCNA (Tập 4): Phần 2
102 trang 227 0 0 -
Bài giảng: Lịch sử phát triển hệ thống mạng
118 trang 227 0 0 -
80 trang 197 0 0
-
Giáo trình Hệ thống mạng máy tính CCNA (Tập 4): Phần 1
122 trang 196 0 0 -
122 trang 191 0 0
-
Giáo trình môn học/mô đun: Mạng máy tính (Ngành/nghề: Quản trị mạng máy tính) - Phần 1
68 trang 183 0 0