The difference between get and post methods in ajax requests

Both get and post are one of the methods of ajax request, and their differences mainly include the following points:
(1) The way of expressing the request is different.
Get is query, post is addition, patch and put are modification, but patch is partial modification, put It is an overall modification, and delete is to delete
(2) The method of passing parameters is different.
Get passing parameters is to add params directly after the url for splicing, that is, directly adding them to the request line, but post passing parameters is to put data in the request body middle

Get/delete pass parameters with params

post/put/patch support both data and params

(3) The security is different. The security of
get is low because its data is exposed in the URL, which is easy to find; the security of post is higher. Generally, login and registration are post, but from the perspective of transmission, they are not It is safe, because http is transmitted in clear text, and https can be used to encrypt
(4) The size of the data is different
. The get parameter has a size limit, because the maximum length that different browsers can support for the address bar is limited, generally 2-5kb; but post There will be no such problem, so the speed of getting parameters is faster than that of post
(5) The parameter types are different.
Get only accepts ASCII characters and post has no restrictions. 

But generally in the project, I write according to the requirements of the interface document. Most of the interface types used in the projects I have done before are post types.


Specific usage examples:

Secondary encapsulation of ajax, which is convenient for the change and maintenance of the base address in the later stage

import axios from 'axios'

const request = axios.create({

  baseURL: 'base address',

  timeout: 5000

})

export default request

send request 

import request from 'request path'

export const add = (data) => {

  return request.post('interface address', data)

}

export const edit = (data) => {

  return request.put(`interface address/${data.id}`, data)

}

export const get= (id) => {

  return request.get(`interface address/${id}`)

}

export const del= (id) => {

  return request.delete(`interface address/${id}`)

}

Guess you like

Origin blog.csdn.net/weixin_48082900/article/details/128450443