Socket
Book a DemoInstallSign in
Socket

ereq

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ereq

一个简单的 Ajax 客服端

1.0.7
latest
Source
npmnpm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

Ereq

一个简单的 Ajax 客服端

用法:

npm install ereq --save

import Ereq from 'ereq'

var globOpts = { baseUrl: 'http://xxx.xx.com' }
var handlers = {
  successHandler: function (resText, resolve, reject) {
    var res = null
    try {
      res = JSON.parse(resText)
    } catch (e) {
      reject(buildErrInfo('PARSE ERROR'))
      return
    }
    if (res.cood === 200) {
      resolve(res.data)
    } else {
      reject(buildErrInfo(res.msg))
    }
  }
}
var req = Ereq(globOpts, handlers)
var options = {
  method: 'post',
  url: 'api/xxxx/xxx',
  params: { name: 'xxx', arg: 'xxxx' }
}

req(options)
.then(res => { console.log('ok', res)})
.catch(err => {console.log('err', err)})

默认的一些请求处理函数

var DEFAULT_HANDLERS = {
  successHandler: function (resText, resolve, reject) {
    var res = null
    try {
      res = JSON.parse(resText)
    } catch (e) {
      reject(buildErrInfo('PARSE ERROR'))
      return
    }
    resolve(res)
  },
  errorHandler: function (xhr, reject) {
    reject(buildErrInfo('REQUEST ERROR', xhr))
  },
  timeoutHandler: function (xhr, reject) {
    reject(buildErrInfo('TIMEOUT', xhr))
  }
}

可以在 Ereq(opionts, handlers) 通过 handlers 进行覆盖

默认的配置项

var DEFAULT_OPTIONS = {
  baseUrl: '',
  timeout: 0,
  method: 'get'
}

额,代码没几行,看源码来得明白~

FAQs

Package last updated on 18 Dec 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.