The usage and differences of the four request methods get, post, put, and delete (all)

get request method

A GET request will send a request for data to the database to obtain information. The request is just like the select operation of the database. It is only used to query the data. It will not modify or increase the data, and will not affect the content of the resource, that is, the request There are no side effects. No matter how many operations are performed, the result is the same.

server

// 传统的URL
app.get('/axios', (req, res) => {
    
    
    res.send('axios get 传递参数' + req.query.id)
})
// Restful 的URL
app.get('/axios/:id', (req, res) => {
    
    
    res.send('axios get(Restful) 传递参数' + req.params.id)
})
app.get('/data', (req, res) => {
    
    
    res.send('Hello World !')
})

Client

axios.get('http://localhost:3000/adata').then(function (ret) {
    
    
            console.log(ret.data);
        });
        // 传统URL 只需要
        axios.get('http://localhost:3000/axios?id=123').then(function (res) {
    
    
            console.log(res.data);
        });
        axios.get('http://localhost:3000/axios/123').then(function (res) {
    
    
            console.log(res.data);
        });
        // 传统URL
        axios.get('http://localhost:3000/axios', {
    
    
            params: {
    
    
                id: 234
            }
        }).then(function (ret) {
    
    
            console.log(ret.data);
        });

delete request method

The DELETE request, as the name implies, is used to delete a certain resource. The request is like the delete operation of the database.

server

// 传统的URL
app.delete('/axios', (req, res) => {
    
    
    res.send('axios delete 传递参数' + req.query.id)
})
// Restful 的URL
app.delete('/axios/:id', (req, res) => {
    
    
    res.send('axios delete(Restful) 传递参数' + req.params.id)
})

Client

axios.delete('http://localhost:3000/axios?id=123').then(function (res) {
    
    
   console.log(res.data);
});
axios.delete('http://localhost:3000/axios/123').then(function (res) {
    
    
   console.log(res.data);
});
axios.delete('http://localhost:3000/axios', {
    
    
   params: {
    
    
       id: 234
   }
}).then(function (res) {
    
    
   console.log(res.data);
});

post request method

A POST request is similar to a PUT request in that it sends data to the server, but the request will change the type of data and other resources, just like the insert operation of the database, it will create new content. Almost all submission operations currently use POST requests.

server

// 传统的URL
app.post('/axios', (req, res) => {
    
    
    res.send('axios post 传递参数' + req.body.id + '---' + req.body.uname + '---' +
        req.body.pwd)
})
// Restful 的URL
app.post('/axios/:id', (req, res) => {
    
    
    res.send('axios post(Restful) 传递参数' + req.params.id + '---' + req.body.uname + '---' +
        req.body.pwd)
})

Client

 axios.post('http://localhost:3000/axios/111', {
    
    
            uname: 'kun',
            pwd: 123,
        }).then(function (res) {
    
    
            console.log(res.data);
        })
        // ----------------------------------------------------------------
        var params = new URLSearchParams();
        params.append('uname', 'ikun');
        params.append('pwd', '111');
        params.append('id', 99999);

        axios.post('http://localhost:3000/axios', params).then(function (res) {
    
    
            console.log(res.data);
        })

put request method

Unlike GET, the PUT request sends data to the server to change the information. The request is like the update operation of the database. It is used to modify the content of the data, but does not increase the type of data, which means that no matter what How many PUT operations are performed, the results are not different.

server

// 传统的URL
app.put('/axios', (req, res) => {
    
    
    res.send('axios put 传递参数' + req.body.id + '---' + req.body.uname + '---' +
        req.body.pwd)
})
// Restful 的URL
app.put('/axios/:id', (req, res) => {
    
    
    res.send('axios put(Restful) 传递参数' + req.params.id + '---' + req.body.uname + '---' +
        req.body.pwd)
})

Client

axios.put('http://localhost:3000/axios/123', {
    
    
            // 传参
            uname: 'lisi',
            pwd: 123
        }).then(ret => {
    
    
            console.log(ret.data);
        })

        /* URLSearchParams 接口定义了一些实用的方法来处理 URL 的查询字符串。
        我们调用new URLSearchParams()会返回一个 URLSearchParams 对象实例 */
        var params = new URLSearchParams();
        params.append('uname', 'kun');
        params.append('pwd', '222');
        params.append('id', 222);

        axios.put('http://localhost:3000/axios', params)
            .then(function (res) {
    
    
                console.log(res.data);
            })

POST, DELETE, PUT, and GET correspond respectively to
add——- delete——- change—— check

Self-motivation

On the road of life, let us always motivate ourselves! Forever! Forever!

Guess you like

Origin blog.csdn.net/weixin_50001396/article/details/112969538