« Axios » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
 
(9 versions intermédiaires par le même utilisateur non affichées)
Ligne 6 : Ligne 6 :
= [https://stackoverflow.com/questions/44877904/how-do-you-import-a-javascript-package-from-a-cdn-script-tag-in-react Client side] =
= [https://stackoverflow.com/questions/44877904/how-do-you-import-a-javascript-package-from-a-cdn-script-tag-in-react Client side] =
<kode lang='html'>
<kode lang='html'>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
    const axios = window.axios;
</script>
</kode>


        <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
= Instance =
        <script>
<kode lang='js'>
            const axios = window.axios;
const instance = axios.create({
        </script>
    baseURL: 'https://www.domain.net/api'
    </body>
});
</html>
</kode>
 
= Authentication =
<kode lang='js'>
const instance = axios.create({
    auth: {
        username: 'xxx',
        password: 'yyy'
    }
});
</kode>
 
= Get =
<kode lang='js'>
instance.get('/item/1')
    .then(function (response) {
        console.log(response);
    })
    .catch(function (error) {
        console.error(error);
    });
 
const response = await instance.get('/item/1');
</kode>
 
= Performing multiple concurrent requests =
<kode lang='js'>
const calls = [];
calls.push(instance.get('/item/1'));
calls.push(instance.get('/item/2'));
 
const responses = await Promise.all(calls);
</kode>
</kode>

Dernière version du 29 janvier 2021 à 21:46

Links

Client side

Html.svg
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
    const axios = window.axios;
</script>

Instance

Js.svg
const instance = axios.create({
    baseURL: 'https://www.domain.net/api'
});

Authentication

Js.svg
const instance = axios.create({
    auth: {
        username: 'xxx',
        password: 'yyy'
    }
});

Get

Js.svg
instance.get('/item/1')
    .then(function (response) {
        console.log(response);
    })
    .catch(function (error) {
        console.error(error);
    });

const response = await instance.get('/item/1');

Performing multiple concurrent requests

Js.svg
const calls = [];
calls.push(instance.get('/item/1'));
calls.push(instance.get('/item/2'));

const responses = await Promise.all(calls);