Web 前端开发

ajax

2018-08-02  本文已影响6人  小7丁

一、ajax 是什么?有什么作用?

二、如何 mock 数据?

server-mock

使用 server-mock node工具启动一个能处理静态文件和动态路由的服务器

用githubpages mock数据

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<script>
  var xhr = new XMLHttpRequest()
  xhr.open('GET','/mock/cate.json',true)
  xhr.send()
  xhr.onload = function(){
    console.log(JSON.parse(xhr.responseText))
  }
</script>
</body>
</html>

在json文件里面写入json格式的内容就可以mock到该数据了

在线上mock数据

例如用Easy mock的网站来mock数据,可以在网站上得到一个URL

三、把视频中GET 和 POST 类型的 ajax 的用法手写一遍,如

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script>
        var xhr = new XMLHttpRequest()
        xhr.open('Get','/login?username=jirengu&password=123',true)
        xhr.send()

        xhr.onreadystatechange = function(){
            if(xhr.readyState === 4 && xhr.status === 200){
                console.log(xhr.responseText)
            }
        }
        //GET的使用
//status 是服务器是否正常
//readystate是交互流程是否结束
//load是readystate是4了

        xhr.addEventListener('readystatechange',function(){
            console.log('readystate:',xhr.readystate)
        })
        xhr.addEventListener('load',function()}{
            console.log(xhr.status)
            if(xhr.status >= 200  && xhr.status < 300 || xhr.status === 304){
                var data = xhr.responseText
                console.log(data)
            }else{
                console.log('error')
            }
        })
        xhr.onerror = function(){
            console.log('error')
        }

//POST 过程

        var xhr = new XMLHttpRequest()
        xhr.open('POST','/login',true)
        xhr.send('username=jirengu&password=123')

        xhr.addEventListener('load',function()}{
            console.log(xhr.status)
            if(xhr.status >= 200  && xhr.status < 300 || xhr.status === 304){
                var data = xhr.responseText
                console.log(data)
            }else{
                console.log('error')
            }
        })
        xhr.onerror = function(){
            console.log('error')
        }
    </script>
</body>
</html>

四、封装一个 ajax 函数,能实现如下方法调用

function ajax(options){
    //补全
}
ajax({
    url: 'http://api.jirengu.com/weather.php',
    data: {
        city: '北京'
    },
    onsuccess: function(ret){
        console.log(ret)
    },
    onerror: function(){
        console.log('服务器异常')
    }
})
//封装一个ajax
function ajax(opts){
    var url = opts.url
    var type = opts.type || 'GET'
    var dataType = opts.dataType || 'json'
    var onsuccess = opts.onsuccess || function(){}
    var onerror = opts.onerror || function(){}
    var data = opts.data || {}

    var dataStr = []
    for(var key in data){
        dataStr.push(key + '=' + data[key])
    }
    dataStr = dataStr.join('&')

    if(type === 'GET'){
        url += '?' + dataStr
    }

    var xhr = new XMLHttpRequest()
    xhr.open(type,url,true)
    xhr.onload = function(){
        if((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304){
            //成功了
            if(dataType === 'json'){
                onsuccess(JSON.parse(xhr.responseText))
            }else{
                onsuccess(xhr.responseText)
            }

    } else{
        xhr.send()
        }
    }
上一篇 下一篇

猜你喜欢

热点阅读