https://github.com/zhangshaolong/service-api
ajax request tool
https://github.com/zhangshaolong/service-api
ajax ajax-request loading promise
Last synced: about 2 months ago
JSON representation
ajax request tool
- Host: GitHub
- URL: https://github.com/zhangshaolong/service-api
- Owner: zhangshaolong
- Created: 2018-09-04T12:29:26.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-01-21T06:56:19.000Z (about 5 years ago)
- Last Synced: 2025-09-20T15:35:00.856Z (6 months ago)
- Topics: ajax, ajax-request, loading, promise
- Language: JavaScript
- Size: 3.27 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# service api tool
ajax request and response a promise
install
npm install service-api --save
```javascript
import service from 'service-api'
// could set config
service.config({
showLoading: () => { to show loading },
hideLoading: () => { to hide loading },
dealError: (error) => {
deal some error
},
checkStatus: (resp) => { // default code == 200 is success request, u can override method for your project
return resp.code === 200
}
})
service.get(path, {key: 'value'}, {
context: document.body // with a loading at the html node
}).then((resp) => {
}).catch((error) => {
})
service.post(path, {key: 'value'}, {
context: document.body
}).then((resp) => {
}).catch((error) => {
})
let defered = service.post(path, {key: 'value'}, {
context: document.body
})
defered.then(() => {
})
// to cancel a ajax
defered.cancel()
// you can set responseType on the third arguments
service.get(path, {key: 'value'}, {
context: document.body,
responseType: 'arraybuffer',
timeout: 30000 // config timeout ms, default 60000ms
}).then((resp) => {
}).catch((error) => {
})
// to clear requests which has send and not received, like to a new page
service.clear()
```