Google Maps API: No ‘Access-Control-Allow-Origin’ header is present on the requested resource

https://maps.googleapis.com/maps/api doesn’t support getting requests from frontend JavaScript running in web apps in the way your code is trying to use it. Instead you must use the supported Google Maps JavaScript API, the client-side code for which is different from what you’re trying. A sample for the Distance Matrix service looks more like: <script> var … Read more

How to return values from async functions using async-await from function? [duplicate]

You cant await something outside async scope. To get expected result you should wrap your console.log into async IIFE i.e async function getData() { return await axios.get(‘https://jsonplaceholder.typicode.com/posts’); } (async () => { console.log(await getData()) })() Working sample. More information about async/await Since axios returns a promise the async/await can be omitted for the getData function … Read more

How to download files using axios

A more general solution axios({ url: ‘http://api.dev/file-download’, //your url method: ‘GET’, responseType: ‘blob’, // important }).then((response) => { const url = window.URL.createObjectURL(new Blob([response.data])); const link = document.createElement(‘a’); link.href = url; link.setAttribute(‘download’, ‘file.pdf’); //or any other extension document.body.appendChild(link); link.click(); }); Check out the quirks at https://gist.github.com/javilobo8/097c30a233786be52070986d8cdb1743 Full credits to: https://gist.github.com/javilobo8

Make Axios send cookies in its requests automatically

You can use withCredentials property. XMLHttpRequest from a different domain cannot set cookie values for their own domain unless withCredentials is set to true before making the request. axios.get(BASE_URL + ‘/todos’, { withCredentials: true }); Also its possible to force credentials to every Axios requests axios.defaults.withCredentials = true Or using credentials for some of the … Read more

axios post request to send form data

You can post axios data by using FormData() like: var bodyFormData = new FormData(); And then add the fields to the form you want to send: bodyFormData.append(‘userName’, ‘Fred’); If you are uploading images, you may want to use .append bodyFormData.append(‘image’, imageFile); And then you can use axios post method (You can amend it accordingly) axios({ … Read more