ihuns

ihuns is http request libs

Browser support

Chrome Firefox Safari Opera Edge IE
Latest ✔ Latest ✔ Latest ✔ Latest ✔ Latest ✔ 8+ ✔

install

npm install ihuns --save

or

<script src="ihuns.js"></script>

import

import ihuns from 'sendjs';

Example

//data request
new Ihuns({
  url: '/api',
  baseURL: 'http://192.168.9.86:8006',
  data: {},
  success: function success(res) {
    resolve(res);
  },
  error: function error(err) {
    reject(err);
  }
});
//file request
new Ihuns({
  url: '/api',
  baseURL: 'http://192.168.9.86:8006',
  data: {},
  isFormData:true,
  success: function success(res) {
    resolve(res);
  },
  error: function error(err) {
    reject(err);
  }
});

//file downClient
new Ihuns({
  url: '/api',
  baseURL: 'http://192.168.9.86:8006',
  data: {},
  isDownClient:true,
  success: function success(res) {
    resolve(res);
  },
  error: function error(err) {
    reject(err);
  }
});

Example

use promise

function createIhuns(url = '', data = {}) {
  return new Promise(function(resolve, reject) {
    new Ihuns({
      url: url,
      baseURL: 'http://192.168.9.86:8006',
      data: data,
      success: function success(res) {
        resolve(res);
      },
      error: function error(err) {
        reject(err);
      }
    });
  });
}
createIhuns('/partner/data', {
  module: 'portal',
  service: 'Portal',
  method: 'access',
  status: '',
  name: '',
  action: 'marketing.MaInfo.getMaInfoList'
}).then(res => {
  console.log(res);
}).catch(err=>{
  console.log(err);
})

Config

url: '', //request server url
method: 'get', //request method
baseURL: '', //server path
headers: {
  'Content-type': 'application/x-www-form-urlencoded; charset=utf-8'
}, //set headers
data: {}, //request params
timeout: null, //set timeout
async: true, //async
withCredentials: false, //Add withCredentials to request if needed
success: function() {}, //success fn
error: function() {}, //error fn
ontimeout: function() {}, //Handle timeout
isFormData: false, //file unload
isDownClient: false //file downClient

response

/**
 * success
 *
 * @param {string} response xhr.responseText
 * @param {number} status xhr.status
 * @param {number} readyState xhr.readyState
 * @param {object} config request config
 */
let res = { response, status, readyState, config}
/**
 * error
 *
 * @param {string} msg error explain
 * @param {object} xhr request xhr
 * @param {object} config request config
 */
let err = (msg, xhr, config)

format

  • umd
  • cjs
  • iife

github

https://github.com/wqb2017/ihuns


sanyang
483 声望10 粉丝

sanyang