使用promise写一个简单的ajax案例:

    1. function ajax (url) {
    2. return new Promise(function (resolve, reject) {
    3. let xhr = new XMLHttpRequest()
    4. xhr.open('GET', url)
    5. xhr.responseType = 'json'
    6. xhr.onload = function () {
    7. if (this.status + '' === '200') {
    8. resolve(this.response)
    9. console.log(this.response)
    10. } else {
    11. reject(new Error(this.responseText))
    12. console.log(this.responseText)
    13. }
    14. }
    15. xhr.send()
    16. })
    17. }
    18. ajax('./ajaxTest.json')