1.0.7 • Published 3 years ago

browser-request-utils v1.0.7

Weekly downloads
2
License
ISC
Repository
github
Last release
3 years ago

browser-request-utils

浏览器通讯类工具,如 jsonp、ajax 封装等

Installment

npm install browser-request-utils --save

Documentation

interface IPromiseAjaxParams {
  method: "get" | "post" | "GET" | "POST";
  url: string;
  data: Record<string, any>;
}

interface IParams extends IPromiseAjaxParams {
  success: (res: any) => void;
  failed: (res: any) => void;
}

interface IJsonpParams {
  url: string;
  charset?: string;
  onsuccess: (res: any) => void;
  onerror?: () => void;
}

ajax

  • @desc ajax 请求工具
  • @param {IParams} params
  • @returns {void}

Usage

  import { ajax } from 'browser-request-utils';

  ajax({
    method: 'get',
    url: 'https://www.npmjs.com/',
    data: {
      mobile: 18165057632,
      page: 2
    },
    success: (res) => {
      console.log(res);
    },
    failed: (error) => {
      console.error(error);
    },
  })

promiseAjax

  • @desc ajax 请求工具,支持 Promise
  • @param {IPromiseAjaxParams} params
  • @returns {void}

Usage

  import { promiseAjax } from 'browser-request-utils';

  promiseAjax({
    method: 'get',
    url: 'https://www.npmjs.com/',
    data: {
      mobile: 18165057632,
      page: 2
    },
  }).then(res => {
    console.log(res);
  }).catch(error => {
    console.error(error);
  })

jsonp

  • @desc jsonp 函数
  • @param {IJsonpParams} params
  • @returns {void}

Usage

  import { jsonp } from 'browser-request-utils';

  jsonp({
    url: 'https://www.npmjs.com/package/browser-request-utils?',
    charset: 'UTF-8',
    onsuccess: (res) => {
      console.log(res);
    },
    onerror: () => {
      // 出错了
    }
  })

  // 需要服务端根据 callback 名称返回一个代码块,例如
  callbackname({
    res: {
      a: 123,
    }
  })
1.0.7

3 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago