Home javascript Axios. How to send data via Post?

Axios. How to send data via Post?

Author

Date

Category

There is a code like this:

data () {
  return {
   loading_cell_phone_number: '',
   loading_zip_code: '',
   loading_city: '',
   loading_street: '',
   loading_house_number: '',
   loading_company_name: '',
   loading_first_name: '',
   loading_surname: '',
  }
 },
methods: {
   sendTrackerClientData: function () {
   axios.post ("https://seo-gmbh.eu/couriertracker/json/couriertracker_api.php?action=tracking.data_save&key_id=", this. $ store.state.tracking_data.key_id +
    {data2: 1}
    , {
      params: {
       user_key_id: this. $ store.state.localStorage.userKeyId,
      },
     headers: {
      'Content-Type': 'application / x-www-form-urlencoded'
     }
    })
    .then (response = & gt; {
    })
    .catch (function (error) {
     console.log (error);
    });
  },
 },

In this example, you can observe an attempt to send data (pay attention to the parameter `{data2: 1}`) to the server – via a Post request.

Instead of sending data from the specified parameter in the form of a key – value – in my case, the following is sent:

 enter image description here

Questions:

1) Why am I passing an empty string as a value, and as a parameter a key along with a value?

2) Is it possible in this case somehow through post – to send data in such a way that the key would be the value: `data2`
and the value of this key was `1` and without any blank lines?


Answer 1, authority 100%

Use the following parameters to customize Axios

axios ({
 url: '...',
 params: {...},
 data: {...}
})

url – URL to be requested

params – URL parameters to be sent with the request

data – data that will be sent as the body of the request. Applies only to request methods PUT , POST , DELETE , and PATCH . When transformRequest is not set, it must be one of the following types:

// - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams
// - Browser only: FormData, File, Blob
// - Node only: Stream, Buffer

PAY ATTENTION TO THE DATA WHICH TYPE DEPENDS ON THE HEADLINES THAT YOU WILL BEND

headers – headers for the request

Here’s an example:

for "Content-type": "application / json; charset = UTF-8"


Answer 2, authority 100%

axios.post ("https://seo-gmbh.eu/couriertracker/json/couriertracker_api.php?action = tracking.data_save & amp; key_id = "+ this. $ store.state.tracking_data.key_id, {
     data2: 1,
   })
   .then (function (response) {
    console.log (response);
   })
   .catch (function (error) {
    console.log (error);
   });

Try it this way data2 = 1 should come to the server


Answer 3

You can send like this:

axios ({
method: 'POST',
        url: 'https://seo-gmbh.eu/couriertracker/json/couriertracker_api.php?action=tracking.data_save&key_id=' + this. $ store.state.tracking_data.key_id,
        params: {data2: 1}
      }). then ((response) = & gt; {
        console.log (response.data);
      })

 enter image description here

Or just transfer data2: 1 to params if I understand correctly

data () {
  return {
   loading_cell_phone_number: '',
   loading_zip_code: '',
   loading_city: '',
   loading_street: '',
   loading_house_number: '',
   loading_company_name: '',
   loading_first_name: '',
   loading_surname: '',
  }
 },
methods: {
   sendTrackerClientData: function () {
   axios.post ("https://seo-gmbh.eu/couriertracker/json/couriertracker_api.php?action=tracking.data_save&key_id=", this. $ store.state.tracking_data.key_id +, {
      params: {
       user_key_id: this. $ store.state.localStorage.userKeyId,
       data2: 1
      },
     headers: {
      'Content-Type': 'application / x-www-form-urlencoded'
     }
    })
    .then (response = & gt; {
    })
    .catch (function (error) {
     console.log (error);
    });
  },
 },

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions