Axios how to use

Introduce Axios how to use

by Ulugbek Aripov

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.0/axios.min.js"></script>
<span class="result"></span>
<button onClick=(getUrl())>
Submit
</button>

JavaScript

const result = document.querySelector('.result');

async function test() {
	getUrl()
	console.log("WORKS !")
}

async function getUrl(){
	console.log("STARTING !")
const {data} = await axios.post('https://landscape-designer-staging.herokuapp.com/api/v1/organizations', {
	email: '[email protected]',
	name: 'Test Mowers',
  phone: '510 234 56 78',
  organization: 'Test Mowers',
  first:'John',
  last:'Doe'
 }, {
    headers: {
      'Content-Type': 'application/json',
    },
  }
).then(function (response) {
	console.log("response-", response)
	let data = response.data;
    	console.log("data-", data)
    result.textContent = data.url
    window.location.replace(data.url);
  })
  .catch(function (error) {
    // handle error
    console.log(error);
  })
  .finally(function () {
    // always executed
    console.log('STOPPED');
  });
}