![]() |
Mise à jour : 02/2015 |
Fiche PHP : PHP et AjaxObjet de la ficheÉtat de la fiche
Plan de la fiche :Avant proposTransférer des informations PHP => Ajax : méthode JSONJSON
JSON est très utile lors des retours AJAX, il permet d'agréer plusieurs types de données en un seul envoi. Structure d'un fichier JSONCoté PHP : Construction et TransmissionCoté Client : Récupération et Exploitation du fichier JSONRécupérationExploitationCreate a JavaScript string containing JSON syntax: var text = '{ "employees" : [' + '{ "firstName":"John" , "lastName":"Doe" },' + '{ "firstName":"Anna" , "lastName":"Smith" },' + '{ "firstName":"Peter" , "lastName":"Jones" } ]}';La syntaxe JSON fait partie du vocabulaire Javascript. On utilise la fonction JSON.parse(text) pour convertir une chaine JSON en un objet javascript. The JavaScript function can be used to convert a JSON text into a JavaScript object: var obj = JSON.parse(text); Use the new JavaScript object in your page: Example p id="demo">/p script document.getElementById("demo").innerHTML = obj.employees[1].firstName + " " + obj.employees[1].lastName; /script |