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");
};

90 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.

  8. supreme clothing

    I wanted to type a message in order to express gratitude to you for all of the pleasant facts you are giving at this site. My incredibly long internet research has at the end of the day been honored with useful facts and strategies to exchange with my friends and family. I would express that many of us visitors actually are very lucky to be in a remarkable community with many outstanding professionals with interesting suggestions. I feel very much happy to have used your entire webpage and look forward to really more amazing times reading here. Thanks a lot once again for all the details.

  9. kd 15

    I needed to put you that very little note so as to thank you very much the moment again for all the pleasant secrets you’ve documented on this site. It was quite unbelievably open-handed with people like you to present unreservedly what exactly many individuals could possibly have distributed as an e-book in making some profit for themselves, precisely considering the fact that you might well have done it if you desired. The things also acted like the easy way to be aware that other individuals have the identical fervor much like my very own to realize great deal more around this matter. I’m sure there are a lot more pleasurable instances in the future for many who find out your website.

  10. supreme clothing

    I have to show my admiration for your kindness in support of people who need assistance with this topic. Your very own dedication to passing the message around turned out to be unbelievably beneficial and have truly enabled individuals just like me to get to their endeavors. This interesting guidelines entails a great deal a person like me and somewhat more to my mates. Thank you; from all of us.

  11. fear of god

    I want to express my respect for your kind-heartedness giving support to those people that should have help with that situation. Your very own dedication to passing the message around appeared to be especially valuable and has always permitted guys and women like me to reach their aims. Your insightful advice can mean a whole lot to me and still more to my office colleagues. Warm regards; from all of us.

  12. kd 13

    I precisely wanted to say thanks yet again. I do not know the things I might have handled in the absence of these techniques provided by you regarding that problem. It was an absolute traumatic crisis in my opinion, but coming across a new specialised avenue you resolved the issue made me to cry for fulfillment. I am just happier for the help and in addition trust you find out what an amazing job you were getting into training many people with the aid of a site. More than likely you have never got to know all of us.

  13. kyrie 7 shoes

    I and my friends have been looking at the great techniques from the blog and then at once came up with a horrible feeling I had not thanked you for those techniques. My boys became so stimulated to read through all of them and have in effect in fact been taking advantage of these things. Many thanks for simply being considerably thoughtful and then for utilizing these kinds of really good guides millions of individuals are really eager to learn about. My personal honest regret for not saying thanks to earlier.

Laisser un commentaire

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