Files
ClassContent/历届学生/刘合群/2019-01-18/ajax.js
2024-09-27 02:06:13 +08:00

98 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 1: ajax封装实现 1
*/
function get(url, callback) {
var http;
http = new XMLHttpRequest();
http.open("GET",url, true);
http.send();
http.onreadystatechange = function () {
if(http.readyState == 4) {
callback(JSON.parse(http.responseText))
}
}
}
/**
* 2: ajax封装实现 2
*/
// (function (win) {
// var $ = function () {},
// http = null;
//
// // 向方法 $ 中追加一个方法 init用来初始化ajax请求
// $.init = function () {
// http = new XMLHttpRequest();
// }
//
// // 向方法 $ 中追加一个方法 get用来发送get请求
// $.get = function (url, callback) {
// $.init()
// http.open("GET",url, true);
// http.send();
// http.onreadystatechange = function () {
// if(http.readyState == 4) {
// callback(http.responseText)
// }
// }
//
// }
// win.$ = $;
// })(window)
/**
* 2: ajax封装实现 2 精简版
*/
// (win => {
// var $ = () => {}, http = null;
//
// // 向方法 $ 中追加一个方法 init用来初始化ajax请求
// $.init = () => http = new XMLHttpRequest();
//
// // 向方法 $ 中追加一个方法 get用来发送get请求
// $.get = (url, callback) => {
// $.init()
// http.open("GET",url, true);
// http.send();
// http.onreadystatechange = () => http.readyState == 4 ? callback(http.responseText) : callback('noData')
// }
// win.$ = $;
// })(window)
// function f(a,b) {
// var result = a + 100;
//
// b(result)
// }
//
// function f1() {
// f(1,function (data) {
// console.log(data)
// } )
// }
//
//