Bien sûr, vous ne vous rappelez pas du fonctionnement de$.ajax()? Nous allons vous montrer un code mettant en application la fonction$.get(). Iâve specified the URL to send the request to as the first parameter and then a settings object as the second parameter. The HTTP GET method is used to retrieve data from the server. The code Iâm talking about is shown below for your convenience: Updating this snippet to employ the $.ajax() function, we obtain the code shown below: Here you can see that I used the first form of the function. Rappelez-vous, nous pouvons recevoir tout et n'importe quoi : du XML, du HTML, du texte, du JSON... peu importe ! Specifically, Iâm going to create a snippet of code that, using the $.ajax() function, retrieves the title and the description of my talk Modern front-end with the eyes of a PHP developer. Et voilà ! Devons-nous envoyer une requête de type POST ou GET ? Pour notre part, nous vous conseillons vraiment d'utiliser les deux raccourcis pour gérer vos appels AJAX dans votre projet. Nous allons devoir faire passer nos variables JavaScript à notre script PHP. In its first form, this function performs an Ajax request using the url parameter and the options specified in settings. HTTP Request: GET vs. POST Let’s breakdown the template is small pieces to understand better. In the second form, the URL is specified in the settings parameter, or can be omitted, in which case the request is made to the current page. C'est très simple, l'argumentdatarevient ici. It sends … The code to achieve this goal is as follows: In the snippet above, I employed several of the properties listed above. C'est logique, lorsque l'appel AJAX aura réussi, cette fonction est automatiquement appelée ; c'est elle que l'on utilise pour mettre à jour notre page ! © Copyright 2020 On vient de gagner un temps fou, pas vrai ? Les paramètrescomplete,successeterrorvont nous être très utiles ! Donnons donc une dimension bien plus concrète à tout cela en créant nos scripts côté serveur. Cette dernière prend un argument : il est automatiquement créé par$.get()et il contient les données reçues du serveur. Et l'objet XmlHttpRequest, il est quand même instancié ? Maria Antonietta Perna is co-Editor of the HTML/CSS Channel at SitePoint and a front-end web developer. C'est un lien relatif, le fichier PHP se trouve donc dans le même répertoire que le fichier JavaScript, et le fichier HTML auquel il est lié. Then we have another script having ready method, The ready() method is used to make a function available after the document is loaded. La fonction$.ajax()va être incontournable pour nos appels AJAX en jQuery, nous l'employons pour envoyer une requête HTTP. Le but est d'obtenirdata : 'valeur1=' + valeur1 + '&valeur2=' + valeur2 + '&valeur3=' + valeur3afin de l'envoyer en AJAX. In the view, we are verifying the ajax request with request.is_ajax() method then if the form is valid we are saving it to the database and returning JSON object with status code and name and for an invalid form, we are returning the form errors with status code 400 i.e bad request.