zoukankan      html  css  js  c++  java
  • 通过ES6实现的Ajax类

    个人学习用途而已,仅供参考。

     1 class Ajax  {
     2     constructor(xhr) {
     3         xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
     4         this.xhr = xhr;
     5     }
     6 
     7     send(method, url, async, callback, data) {
     8         let xhr = this.xhr;
     9 
    10         xhr.onreadystatechange = () => {
    11             // readyState: 0: init, 1: connect has set up, 2: recive request, 3: request.. , 4: request end, send response
    12             if (xhr.readyState === 4 && xhr.status === 200) {
    13                 // status: 200: OK,  404: Not Found Page
    14                 callback(xhr.responseText);
    15             }
    16         };
    17 
    18         xhr.open(method, url, async);   
    19         xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    20         xhr.send(data);
    21     }
    22 }

    Promise增强版:

     1 class Ajax  {
     2     constructor(xhr) {
     3         xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
     4         this.xhr = xhr;
     5     }
     6 
     7     send(options) {
     8 
     9         let xhr = this.xhr;
    10 
    11         let opt = {
    12             type: options.type || 'get',
    13             url: options.url || '',
    14             async: options.async || true,
    15             dataType: options.dataType || 'json',
    16             questring: options.questring || ''
    17         };
    18 
    19         return new Promise((resolve, reject) => {
    20 
    21             xhr.open(opt.type, opt.url, opt.async);
    22 
    23             xhr.onreadystatechange = () => {
    24                 // readyState: 0: init, 1: connect has set up, 2: recive request, 3: request.. , 4: request end, send response
    25                 if (xhr.readyState === 4) {
    26                     if (xhr.status === 200) {
    27                         // status: 200: OK,  404: Not Found Page
    28                         if (opt.dataType === 'json') {
    29                             const data = JSON.parse(xhr.responseText);
    30                             resolve(data);
    31                         }
    32                     } else {
    33                         reject(new Error(xhr.status || 'Server is fail.'));
    34                     }
    35                 }
    36             };
    37 
    38             xhr.onerror = () => {
    39                 reject(new Error(xhr.status || 'Server is fail.'));
    40             }
    41 
    42             xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    43             xhr.send(opt.questring);
    44 
    45         });
    46     }
    47 }

    点击获取源码

  • 相关阅读:
    获取数据——爬取某微博评论
    使用Microsoft Power BI进行基本的数据分析
    Hadoop分布式文件系统
    使用Visual Studio开发Python
    ML:吴恩达 机器学习 课程笔记(Week5~6)
    ML:吴恩达 机器学习 课程笔记(Week7~8)
    ML:吴恩达 机器学习 课程笔记(Week9~10)
    知乎用户报告
    ML:多变量代价函数和梯度下降(Linear Regression with Multiple Variables)
    ML:吴恩达 机器学习 课程笔记(Week1~2)
  • 原文地址:https://www.cnblogs.com/tim100/p/6807177.html
Copyright © 2011-2022 走看看