欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ES6---继续RESTful

程序员文章站 2022-03-22 20:45:34
ES6 继续RESTful 1. POST请求,添加数据 function addTodo() { console.log('POST 请求'); axios.post('http://jsonplaceholder.typicode.com/todos', { "title": "xiaomin ......

es6---继续restful

 

1. post请求,添加数据

function addtodo() {
    console.log('post 请求');
    axios.post('http://jsonplaceholder.typicode.com/todos', {
        "title": "xiaomin is reading",
        "completed": false
    }).then(result => {
        console.log(result);
    }).catch(error => console.log(error));
}

 

console:

ES6---继续RESTful

 

 

2. put,在url后面加上id

//put/patch请求
function updatetodo() {
    console.log('put/patch请求');
    axios.put('http://jsonplaceholder.typicode.com/todos/1', {
        title: "xiaomin is reading",
        completed: false
    }).then(result => {
        console.log(result);
    }).catch(error => {
        console.log(error);
    })
}

 

console:

ES6---继续RESTful

 

 

3. patch请求

//put/patch请求
function updatetodo() {
    console.log('put/patch请求');
    axios.patch('http://jsonplaceholder.typicode.com/todos/1', {
        userid: 1,
        title: "xiaomin is reading",
        completed: false
    }).then(result => {
        console.log(result);
    }).catch(error => {
        console.log(error);
    })
}

 

console:

ES6---继续RESTful

 

 

 

4. delete

//delete请求
function removetodo() {
    console.log('delete 请求');
    //ajax里面是 url?id=1   ===>url/1
    axios.delete('http://jsonplaceholder.typicode.com/todos/1')
        .then(result => { console.log(result); })
        .catch(error => { console.log(error); })
}

 

console:

ES6---继续RESTful

 

 

5. 批量请求数据

//批量请求数据
function getdata() {
    console.log('批量请求数据');
    axios.all([
        axios.get('file:///c:/users/administrator/desktop/bstudy/1111.json'),
        axios.get('file:///c:/users/administrator/desktop/bstudy/1112.json')
    ]).then(result => {
        console.log(result);
    });
}

 

console:

ES6---继续RESTful

 

 

ES6---继续RESTful

 

 

6. 

//批量请求数据
function getdata() {
    console.log('批量请求数据');
    axios.all([
        axios.get('file:///c:/users/administrator/desktop/bstudy/1111.json'),
        axios.get('file:///c:/users/administrator/desktop/bstudy/1112.json')
    ]).then(axios.spread((a1, a2) => { //分发数据
        console.log(a1.data);
        console.log(a2.data);
    }))
}

 

console:

ES6---继续RESTful

 

 

7.