superajax v1.1.6
SuperAjax for Browser
SuperAjax is a powerful tool of ajax for browser. It supported IE6+,FF, Chrome, Safari and so on. It should work in
Babel
or orther compiler.
Installation
npm i superajax --save
Usage
import superajax from 'superajax'
superajax.get('example.com')
.end()
.then(response => {
console.log(response)
// the result will like this
// {
// status: Numer,
// statusText: '',
// respoonse: {url: '', headers: {}},
// request: {url: '', headers: {}},
// error: Error || null,
// text: '',
// body: Object || null
// }
})
// or
superajax.get('example.com')
.end((err, response) => {
console.log(response)
})
API
.version | .release
return the version info of this module
.get(url)
- url
<String>
Which to set request url, and request type as
GET
.
.post(url)
- url
<String>
Which to set request url, and request type as
POST
.
.jsonp(url)
- url
<String>
Which to set request url, and request type as
JSONP
.
.type(typeName)
- typeName
<String>
, 'form'/'json', default 'form'
Set the content-type of this Form, set 'form' as 'application/x-www-form-urlencoded', and 'json' as 'application/json'. But if .field() method is using, this change type method will useless
superajax.get(url)
.type('json')
.send({foo: 'bar'}) // will be formated by JSON.stringify()
.end()
.set(key, val)
- key
<String> | <Object>
- val
<String>
, optional
Which to set request headers, ignore the uppercase letter.
superajax.get(url)
.set('x-request-foo', 'bar')
.set('x-request-aaa', 'bbb')
// or .set({'x-request-foo', 'bar', 'x-request-aaa': 'bbb'})
.end()
.send(key, val)
- key
<String> | <Object>
- val
<Any>
, optional
Which to send data to server.
superajax.post(url)
.send('foo', 'bar')
.send('aaa', 123)
// or .send({foo, 'bar', aaa: 123})
.end()
// or
superajax.post(url)
.send('It is a long story.')
.type('json')
.end()
.field(key, val)
- key
<String> | <Object>
- val
<Any>
, optional
Which to send blob data to server. its formType will auto be set to 'form-data', and can not change any more.
let file = $fileDom.files[0] // like <input type="file" />
superajax.post(url)
.field('file', file)
.field('foo', 'bar') // can also send normal data
// or .field({foo, 'bar', file: file})
.end()
.cache(bool)
bool
<Boolean>
, defaultfalse
Cache the data which it's set to be true, in http GET.
.abort()
Which to abort this request. It will not return the response in JSONP.
.timeout(ms)
- ms
<Number>
, microsecond.
This request will be aborted if not receives response in the time you set. The end method won't be triggered in JSONP.
superajax.get(url)
.timeout(5000)
.end()
.form(dom)
- dom
<Document Object>
Which to send a form existed. Like
<form>...</form>
.
var $form = document.queryselector('form')
superajax.post(url)
.form($form)
.end()
.end(callback)
- callback
<Funtion>
, optional
Which to trigger the request. This method is required for all request. The param
callback
will be triggered on the end of response received. If callback is not set, if will return aPromise Object
.