美文网首页
ES6:Promise 实用

ES6:Promise 实用

作者: 开车去环游世界 | 来源:发表于2016-12-24 11:11 被阅读72次
'use strict';
import Promise from './es6-promise.min';

module.exports = {
  PAGE_WORK: '',

  get (url) {
    return new Promise((resolve, reject) => {
      console.log(url)
      wx.request({
        url: url,
        headers: {
          'Content-Type': 'application/json'
        },
        success: (res) => {
          resolve(res);
        },
        fail: (res) => {
          reject(res);
        }
      })
    })
  },

  post (url, data) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: url,
        data: data,
        method: 'POST',
        headers: {
          'Cache-Control': 'no-cache',
          'Content-Type': 'application/x-www-form-urlencode;charset=UTF-8;'
        },
        success: (res) => {
          resolve(res);
        },
        fail: (res) => {
          reject(res);
        }
      })
    })
  },

  json2Form(json) {
    let str = [];
    for( let p in json ) {
      str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p]));
    }
    return str.join( "&" );
  }

};

相关文章

网友评论

      本文标题:ES6:Promise 实用

      本文链接:https://www.haomeiwen.com/subject/hqmlvttx.html