« Axios » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
 
(4 versions intermédiaires par le même utilisateur non affichées)
Ligne 10 : Ligne 10 :
     const axios = window.axios;
     const axios = window.axios;
</script>
</script>
</kode>
= Instance =
<kode lang='js'>
const instance = axios.create({
    baseURL: 'https://www.domain.net/api'
});
</kode>
</kode>


Ligne 15 : Ligne 22 :
<kode lang='js'>
<kode lang='js'>
const instance = axios.create({
const instance = axios.create({
    baseURL: 'https://www.domain.net/api',
     auth: {
     auth: {
         username: 'xxx',
         username: 'xxx',
Ligne 25 : Ligne 31 :
= Get =
= Get =
<kode lang='js'>
<kode lang='js'>
instance.get('/item?api-version=1')
instance.get('/item/1')
     .then(function (response) {
     .then(function (response) {
         console.log(response);
         console.log(response);
Ligne 33 : Ligne 39 :
     });
     });


const getItemById = async (id) =>
const response = await instance.get('/item/1');
    azure.get(`/item/${id}`)
</kode>
        .then(function (response) {
 
            const item = response.data.value;
= Performing multiple concurrent requests =
            return item;
<kode lang='js'>
        })
const calls = [];
        .catch(function (error) {
calls.push(instance.get('/item/1'));
            console.error(error);
calls.push(instance.get('/item/2'));
        });


let item = await getItemById('1');
const responses = await Promise.all(calls);
$("#result").html(JSON.stringify(item, null, 4));
</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);