xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

how to make one your own promise version Ajax

how to make one your own promise version Ajax

XMLHttpRequest

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest

download

upload

"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2020-05-18
 * @modified
 *
 * @description promise ajax
 * @augments
 * @example
 * @link
 *
 */

const log = console.log;

const url = `https://cdn.xgqfrms.xyz/json/cat.json`;

const callback = () => {
  log(`callback`)
}

const onLoad = () => {
  log(`onload`)
}


const ajax = new XMLHttpRequest();
ajax.open("HEAD", url);
ajax.callback = callback;
ajax.filepath = url;
ajax.onload = onLoad;
ajax.send();


AbstractFactory & Ajax

Promise


Fetch API

jQuery Ajax


function doTheThing() {
  return new Promise((resolve, reject) => {
    $.ajax({
      url: window.location.href,
      type: 'POST',
      data: {
        key: 'value',
      },
      success: function(data) {
        resolve(data)
      },
      error: function(error) {
        reject(error)
      },
    })
  })
}



©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


posted @ 2020-05-18 20:44  xgqfrms  阅读(151)  评论(5编辑  收藏  举报