1.0.2 • Published 4 years ago

jsonp-good v1.0.2

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

jsonp-good

一个用jsonp发送跨域请求的模块,返回一个Promise对象。

安装

$ npm install jsonp-good

参数说明

jsonpG({
  url: 'http://xxxx/xxxx/',
  funcName: 'fake',
  timeout: 3000,
  params: {
    a: '1',
    b: '2'
  }
})
  .then(res => console.log(res))
  .catch(e => console.log(e))
  • url必选参数 !!!请求的完整地址
  • funcName必选参数!!!回调函数的名字,随便叫什么都可以,总之必须要写,而且要保证该名字是全局唯一的
  • timeout: 可选参数,超时时间设置,单位是 ms,默认值 6000
  • params:可选参数,就是所谓的 query string 啦~
// 支持的params形式,为了方便演示,url使用了相对路径
// 不传params,最终url: /foo?callback=fake
jsonpG({
    url: '/base/get'
    funcName: 'fake'
})


// 普通
jsonpG({
    url: 'http://xxxx/xxxx.com',
    funcName: 'fake',
    params: {
        foo: 'bar'
    }
})

// 数组
jsonpG({
    url: 'http://xxxx/xxxx.com',
    funcName: 'fake',
    params: {
        foo: ['bar', 'baz']
    }
})

// 对象
jsonpG({
    url: 'http://xxxx/xxxx.com',
    funcName: 'fake',
    params: {
        foo: {
      		bar: 'baz'
    	}
    }
})

// Date 类型
jsonpG({
    url: 'http://xxxx/xxxx.com',
    funcName: 'fake',
    params: {
        date
    }
})

// 特殊字符支持, 对于字符 @、:、$、,、、[、],允许出现在 url 中的,不会被 encode
jsonpG({
    url: 'http://xxxx/xxxx.com',
    funcName: 'fake',
    params: {
        foo: '@:$, '
    }
})

// 空值忽略
jsonpG({
    url: 'http://xxxx/xxxx.com',
    funcName: 'fake',
    params: {
        foo: 'bar',
    	baz: null
    }
})

// 丢弃 url 中的哈希标记
jsonpG({
    url: '/base/get#hash',
    funcName: 'fake',
    params: {
		foo: 'bar'
    }
})