Why the axios request was not successful

< script src= " https://unpkg.com/axios/dist/...;></script>

    <script src="./js/Jquery.js"></script>
    <script>
        $.ajax({
            url: "http://betai.yiboshi.com/sign/apply/info/addApplyInfo",
            type: "POST",
            data:{
                test:22
            },
            dataType: "json",
            success: function(data) {
            
            }
        });
        axios.post("http://beta.yiboshi.com/sign/apply/info/addApplyInfo",{
            test:222
        })
        .then(function (response) {
            console.log(response);
        })
        .catch(function (error) {
            console.log(error);
        });
        axiosaxios
Apr.25,2022

are you sure it's the same address:

clipboard.png


check whether axios is introduced. If global this is mounted.


I guess it's because of the cors strategy.

  1. axios defaults to content-type is application/json , which is a non-simple request that triggers options prechecking. And you did not do the corresponding response strategy to cross-domain.
  2. while ajax defaults to application/x-www-form-urlencoded does not trigger predetection, and the backend can receive it.

solution:

  1. change content-type 2. The background does options pre-check request processing.

qs
qs

    npm install axios

main.js
//qs

    import qs from 'qs'

//baseURL

    Axios.defaults.baseURL = 'https://route.showapi.com';

//

    Axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

vue

    this.$axios.post('/25-3',qs.stringify({
       //
      }))
      .then(rsp=>{
      })
      .catch(error=>{
      })
    }     

should be caused by the two different formats of data sent by the backend.


let param = new URLSearchParams ()
param.append ('test',222)
post pass parameters like this.


compare the request body of ajax and axios


axios post request to install plug-in qs

Menu