Axios GET POST Pass parameters and usage methods. Vue is used as an example

First, a brief introduction to Axios

Axios is a PROMISe-based HTTP client for browsers and NodeJS that itself has the following characteristics:

  • Create the XMLHttpRequest from the browser
  • Make an HTTP request from Node.js
  • Supporting Promise API
  • Intercept requests and responses
  • Transform the request and response data
  • Cancel the request
  • Automatically convert JSON data
  • The client supports CSRF/XSRF prevention

A get request

  • The essence of GET is to GET.

  • Data is obtained from the server, which is more efficient than POST.

  • GET requests can be cached

  • In the HTTP protocol definition, there is no limit to the data size of the GET request, but it is generally limited to between 2 and 8K because of different browsers.

  • When GET sends a request, all parameters (query strings) in the URL are wrapped in the URL except the resource path

  • Parameter format

    • Add at the end of the resource path?Indicates the appended parameter.
    • Each variable and value according toVariable name = variableThe value cannot contain Spaces or Chinese characters.
    • Multiple parameter usage&The connection.
    • Note: If the URL string contains Spaces or Chinese, you need to add a percent escape

Get use method and pass parameter

    // get Gets the data
   export let getStats = (a) = > { 
       return axios({ 
           url: "/login".method: "get"./ / the refs
           params: { user: '123123'}}); }Copy the code

A post request

  • The essence of POST is to give.
  • Sending data to the server can also GET the result after the server processing, which is not as efficient as GET.
  • POST requests cannot be cached.
  • POST data is relatively large, and the size is limited by the server Settings. PHP usually limits the size to 2M.
  • When a POST request is sent, the URL contains only the resource path but no parameters. Server logs do not record parameters, which is relatively safer.
  • The arguments are wrapped as binary data bodies in the same format as GET except that? .
  • Note: All privacy related data (password, bank card number) must remember to use POST mode.

Use method of POST and pass parameters

   // Post gets the data
   export let postStats = (a) = > { 
       return axios({ 
           url: "/regiser".method: "post"./ / the refs
           data: { user: '123123'}}); }Copy the code

Note: Get and POST are passed differently in AXIos