Loading...
Développement-code

JSON et JSONP avec PHP

Voici comment récupérer des données au format JSON en PHP avec JQUery.

JSON (JavaScript Object Notation) est un format de données textuel, générique, dérivé de la notation des objets du langage ECMAScript. Il permet de représenter de l’information structurée. Créé par Douglas Crockford, il est décrit par la RFC 4627 de l’IETF.
Un document JSON ne comprend que deux éléments structurels :

des ensembles de paires nom / valeur ;
des listes ordonnées de valeurs.

Ces mêmes éléments représentent 3 types de données :

des objets ;
des tableaux ;
des valeurs génériques de type tableau, objet, booléen, nombre, chaîne ou null.

Code côté PHP json.php

< ?php header('Cache-Control: no-cache, must-revalidate'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Content-type: application/json'); $data[1]['id'] = "1"; $data[1]['value'] = "value1"; $data[2]['id'] = "2"; $data[2]['value'] = "value2"; echo json_encode($data); ?>

Page HTML appelant avec JQuery


$.ajax({
url: "json.php",
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: onLoadData
});
function onLoadData(data) {
alert("I load data");
};

JSONP pour JSON with padding (en français, « JSON avec formatage ») est une extension dans laquelle le nom de la fonction de retour est indiqué. L’idée originale proviendrait du blog MacPython en 2005 et est maintenant répandu dans les applications Web 2.0, telle que Google’s toolkit Applications. Des améliorations sont prévues pour y ajouter des arguments.

JSONP utilise des balises de scripts, les appels sont ouverts. Ainsi, JSONP peut être inapproprié pour transporter des informations confidentielles.

Code côté PHP jsonp.php

< ?php header('Cache-Control: no-cache, must-revalidate'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Content-type: application/json'); $data[1]['id'] = "1"; $data[1]['value'] = "value1"; $data[2]['id'] = "2"; $data[2]['value'] = "value2"; echo $_GET['callback'] . '('.json_encode($data).')'; ?>


$.ajax({
url: "jsonp.php",
contentType: "application/json; charset=utf-8",
dataType: 'jsonp',
success: onLoadData
});
function onLoadData(data) {
alert("I load data");
};

64 comments
  1. kawhi leonard shoes

    I simply had to thank you so much all over again. I do not know what I could possibly have gone through without those recommendations discussed by you regarding that topic. It was actually a intimidating difficulty in my opinion, but being able to see the very skilled approach you handled it forced me to jump for delight. I will be happy for this support and as well , expect you realize what an amazing job you are always doing educating the rest by way of your webpage. I know that you have never got to know all of us.

  2. bape clothing

    I have to point out my respect for your kindness in support of men who should have assistance with this particular subject. Your real commitment to getting the message across became amazingly effective and have in every case made others like me to get to their objectives. Your amazing valuable publication signifies much to me and far more to my colleagues. With thanks; from each one of us.

  3. paul george shoes

    I have to voice my passion for your generosity giving support to those individuals that should have guidance on this particular subject. Your special dedication to getting the solution around appears to be extremely practical and have empowered regular people like me to get to their objectives. Your own invaluable facts indicates this much to me and even more to my colleagues. Regards; from all of us.

  4. golden goose

    I wanted to post you the little bit of note so as to thank you so much again for your extraordinary tactics you’ve featured above. It is certainly seriously open-handed of people like you to deliver freely all that most of us would’ve distributed for an e book to end up making some cash for their own end, even more so considering the fact that you could possibly have tried it in case you decided. These solutions in addition worked as a fantastic way to comprehend someone else have the same interest like my own to learn significantly more regarding this matter. I think there are some more pleasurable opportunities in the future for individuals who check out your site.

  5. ggdb sneakers

    Needed to compose you the tiny observation in order to give many thanks once again for your personal magnificent solutions you have featured on this page. This has been pretty open-handed of you to allow without restraint all that a lot of folks could possibly have offered as an ebook to help with making some profit for their own end, precisely now that you might well have tried it if you ever desired. Those tips in addition served as a great way to be aware that someone else have a similar zeal like my very own to realize more and more pertaining to this issue. Certainly there are many more pleasant periods ahead for folks who looked over your site.

  6. kyrie irving shoes

    I wish to get across my respect for your kind-heartedness in support of women who should have assistance with that area. Your personal dedication to passing the solution throughout has been extraordinarily interesting and have usually helped professionals much like me to achieve their endeavors. Your new informative facts indicates a great deal to me and extremely more to my fellow workers. Many thanks; from everyone of us.

  7. golden goose sneakers

    I simply had to say thanks once again. I am not sure the things I would’ve created without these recommendations shared by you relating to that concern. This was a very depressing circumstance in my position, but spending time with a specialized avenue you processed that made me to leap over delight. Extremely happier for this service and hope you really know what an amazing job that you are getting into training the rest using your website. Probably you’ve never got to know any of us.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée.