Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/damonare/ajax
ajax without jquery
https://github.com/damonare/ajax
ajax javascript
Last synced: 3 months ago
JSON representation
ajax without jquery
- Host: GitHub
- URL: https://github.com/damonare/ajax
- Owner: damonare
- Created: 2017-01-18T14:26:18.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-11-13T02:48:26.000Z (about 6 years ago)
- Last Synced: 2023-10-20T11:51:41.657Z (over 1 year ago)
- Topics: ajax, javascript
- Language: JavaScript
- Homepage:
- Size: 6.84 KB
- Stars: 39
- Watchers: 2
- Forks: 13
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
### 轻量级ajax
- 单独对ajax进行了封装,轻量,方便使用。
- 如果您使用jQuery只是为了使用`$ajax()`方法的话,这个正适合您。### 例:
#### Get请求
```javascript
ajax.get(url, {
name: 'damonare',
sex: 'man'
}, function(res){
console.log(res);
}, async);
// 或
ajax.get(url, {
name: 'damonare',
sex: 'man'
success: function(res) {},
error: function(err) {}
});
```#### POST请求
```javascript
// json
ajax.post('/', {
data: {
"name": "damonare",
"age": 12
},
dataType: 'json',
contentType: 'application/json; charset=UTF-8',
success: function(res) {
console.log(res);
},
error: function(err) {
console.error(err);
}
});
// x-www-form-urlencoded
ajax.post('/', {
data: {
"name": "damonare",
"age": 12
},
dataType: 'json',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
success: function(res) {
console.log(res);
},
error: function(err) {
console.error(err);
}
});
// form-data
ajax.post('/', {
data: formData,
dataType: 'json',
success: function(res) {
console.log(res);
},
error: function(err) {
console.error(err);
}
});
```#### XMLHttpRequest实例对象获取
```javascript
// 实例对象
ajax.post('/', {
data: {},
xhr: function(xhr) {
console.log(xhr);
}
});
```#### 同步请求(慎用)
```javascript
// 实例对象
ajax.get('/', {
data: {}
}, function() {
}, false);
```