1. var api = 'https://api.github.com/users/torvalds'
    2. var xhr = new XMLHttpRequest() // 创建XMLHttpRequest对象
    3. if(window.XMLHttpRequest){ // 兼容处理
    4. xhr = new XMLHttpRequest()
    5. }else{
    6. xhr = new ActiveXObject('Microsoft.XMLHTTP')// 兼容ie6以下下
    7. }
    8. xhr.open('get',api,true) //设置请求信息
    9. xhr.send() //提交请求
    10. //等待服务器返回内容
    11. xhr.onreadystatechange = function() {
    12. if ( xhr.readyState == 4 && xhr.status == 200 ) {
    13. console.log(JSON.parse(xhr.responseText)) // 使用JSON.parse解析JSON字符串
    14. }
    15. }