CONNEXION
  • RetourJeux
    • Sorties
    • Hit Parade
    • Les + populaires
    • Les + attendus
    • Soluces
    • Tous les Jeux
    • Gaming
  • RetourActu Gaming
    • News
    • Astuces
    • Tests
    • Previews
    • Toute l'actu gaming
  • RetourBons plans
    • Bons plans
    • Bons plans Smartphone
    • Bons plans Hardware
    • Bons plans Image et Son
    • Bons plans Amazon
    • Bons plans Cdiscount
    • Bons plans Decathlon
    • Bons plans Fnac
    • Tous les Bons plans
  • RetourJVTech
    • Actus High-Tech
    • Intelligence Artificielle
    • Smartphones
    • Mobilité urbaine
    • Hardware
    • Image et son
    • Tutoriels
    • Tests produits High-Tech
    • Guides d'achat High-Tech
    • JVTech
  • RetourCulture
    • Actus Culture
    • Culture
  • RetourVidéos
    • A la une
    • Gaming Live
    • Vidéos Tests
    • Vidéos Previews
    • Gameplay
    • Trailers
    • Chroniques
    • Replay Web TV
    • Toutes les vidéos
  • RetourForums
    • Hardware PC
    • PS5
    • Switch 2
    • Xbox Series
    • Switch
    • Pokemon pocket
    • FC 25 Ultimate Team
    • League of Legends
    • Tous les Forums
  • PC
  • PS5
  • Xbox Series
  • Switch 2
  • PS4
  • One
  • Switch
  • iOS
  • Android
  • MMO
  • RPG
  • FPS
En ce moment Genshin Impact Valhalla Breath of the wild Animal Crossing GTA 5 Red dead 2
Liste des sujets

Convertir une requete AJAX en JQUERY en JAVASCRIPT VANILLA

Lolopy
Lolopy
Niveau 67
28 avril 2023 à 19:44:15
$.ajax({
        method: "POST",
        dataType: "JSON",
        data: { [input.name]: input.value },
        url: G_SITE_URL + "index.php/profil/edit_" + input.name,
        success: function (msg) {
            if (msg.success == true) {
                // Get the field name container and update the text
                const fieldNameContainer = document.getElementById(element.dataset.fieldName);
                fieldNameContainer.innerText = msg.display;

                // Get the field container for hide the error inside
                const fieldClassContainer = document.getElementsByClassName(element.dataset.fieldName)[0];
                const inputError = fieldClassContainer.getElementsByClassName('input-error')[0];
                inputError.style.display = "none";
            } else {
                // Get the field container for show an error inside
                const fieldClassContainer = document.getElementsByClassName(element.dataset.fieldName)[0];
                const inputError = fieldClassContainer.getElementsByClassName('input-error')[0];
                inputError.style.display = "block";
            }
        }
    });

Bonjour,

Pouvez vous m'aider pour convertir cette requête?

fazow
fazow
Niveau 74
28 avril 2023 à 20:24:19

La doc de la fonction fetch() qui permet de faire des requêtes en vanilla JS

https://developer.mozilla.org/fr/docs/Web/API/Fetch_API/Using_Fetch

EnVrai
EnVrai
Niveau 6
02 mai 2023 à 18:18:37

Tu fais un copié collé de ton post à Chat GPT, t'as ta réponse.

Marav
Marav
Niveau 50
04 mai 2023 à 03:32:43

En plus de la réponse de fazow qui est une solution parmi d'autres, il faut que tu regardes un peu la documentation de jQuery qui de mémoire est pas trop mal foutue.
Comme ça tu pourras de toi même faire le lien.

Pseudo supprimé
Pseudo supprimé 04 mai 2023 à 08:56:00
const xhr = new XMLHttpRequest();
const url = G_SITE_URL + "index.php/profil/edit_" + input.name;
const params = JSON.stringify({ [input.name]: input.value });

xhr.open("POST", url);
xhr.setRequestHeader("Content-type", "application/json");
xhr.onreadystatechange = function () {
    if (xhr.readyState === 4 && xhr.status === 200) {
        const msg = JSON.parse(xhr.responseText);
        if (msg.success == true) {
            // Get the field name container and update the text
            const fieldNameContainer = document.getElementById(element.dataset.fieldName);
            fieldNameContainer.innerText = msg.display;

            // Get the field container for hide the error inside
            const fieldClassContainer = document.getElementsByClassName(element.dataset.fieldName)[0];
            const inputError = fieldClassContainer.getElementsByClassName('input-error')[0];
            inputError.style.display = "none";
        } else {
            // Get the field container for show an error inside
            const fieldClassContainer = document.getElementsByClassName(element.dataset.fieldName)[0];
            const inputError = fieldClassContainer.getElementsByClassName('input-error')[0];
            inputError.style.display = "block";
        }
    }
};
xhr.send(params)
Lolopy
Lolopy
Niveau 67
04 mai 2023 à 10:56:41

Le 04 mai 2023 à 08:56:00 :
const xhr = new XMLHttpRequest(); const url = G_SITE_URL + "index.php/profil/edit_" + input.name; const params = JSON.stringify({ [input.name]: input.value }); xhr.open("POST", url); xhr.setRequestHeader("Content-type", "application/json"); xhr.onreadystatechange = function () { if (xhr.readyState === 4 && xhr.status === 200) { const msg = JSON.parse(xhr.responseText); if (msg.success == true) { // Get the field name container and update the text const fieldNameContainer = document.getElementById(element.dataset.fieldName); fieldNameContainer.innerText = msg.display; // Get the field container for hide the error inside const fieldClassContainer = document.getElementsByClassName(element.dataset.fieldName)[0]; const inputError = fieldClassContainer.getElementsByClassName('input-error')[0]; inputError.style.display = "none"; } else { // Get the field container for show an error inside const fieldClassContainer = document.getElementsByClassName(element.dataset.fieldName)[0]; const inputError = fieldClassContainer.getElementsByClassName('input-error')[0]; inputError.style.display = "block"; } } }; xhr.send(params)

J'ai oublié de dire sans XMLHttpRequest. Mais en utilisant la méthode de nos jours.

Frigid
Frigid
Niveau 62
23 mai 2023 à 20:31:31

Personne ?

Bolge_VIII
Bolge_VIII
Niveau 6
24 mai 2023 à 19:01:49

Le 23 mai 2023 à 20:31:31 :
Personne ?

fetch(G_SITE_URL + "index.php/profil/edit_" + input.name, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({ [input.name]: input.value })
})
.then(response => response.json())
.then(msg => {
    if (msg.success == true) {
        // Get the field name container and update the text
        const fieldNameContainer = document.getElementById(element.dataset.fieldName);
        fieldNameContainer.innerText = msg.display;

        // Get the field container for hide the error inside
        const fieldClassContainer = document.getElementsByClassName(element.dataset.fieldName)[0];
        const inputError = fieldClassContainer.getElementsByClassName('input-error')[0];
        inputError.style.display = "none";
    } else {
        // Get the field container for show an error inside
        const fieldClassContainer = document.getElementsByClassName(element.dataset.fieldName)[0];
        const inputError = fieldClassContainer.getElementsByClassName('input-error')[0];
        inputError.style.display = "block";
    }
})
.catch(error => console.error('Erreur :', error));

pas testé

Salut-les-khey6
Salut-les-khey6
Niveau 8
24 mai 2023 à 19:41:58

Chat gpt est ton amis

Sous forums
  • Aide à l'achat Mac
  • Macintosh
  • Création de Jeux
  • Programmation
  • Création de sites web
  • Linux
  • Internet
  • Steam Deck
  • Hardware
La vidéo du moment