# ajax

function ajax(method,url,params,callback) {
  //对参数进行处理
  method=method.toUpperCase()
  let post_params=null
  let get_params=''
  
  if (method==='GET') {
    if (typeof params==='object') {
      let tempArr=[]
      for (let key in params) {
        tempArr.push(`${key}=${params[key]}`)
      }
      params=tempArr.join('&')
    }
    get_params=`?${params}`
  } else {
    post_params=params
  }

  //发请求
  let xhr=new XMLHttpRequest()

  xhr.onreadystatechange=function(){
    if (xhr.readyState!==4) return
    callback(xhr.responseText)
  }

  xhr.open(method,url+get_params,false)
  if (method==='POST')
    xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')

  xhr.send(post_params) 
}

ajax('get','https://www.baidu.com',{id:15},data=>console.log(data))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35