Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Jackie Jen
Jackie Jen
2,723 Points

Is there any example to show the different of using ajax and json in php?

I have read the differences between ajax and json form google but still don't understand how both ajax and json work with php

2 Answers

Kevin Korte
Kevin Korte
28,148 Points

I'm not sure of a example to show, but what's got you confused exactly?

Ajax is a way of sending data to the server, having the server process it, and than send back to us it's response, and change the page without forcing a refresh of the page.

It basically cuts out a page refresh to communicate with the server, which tends to increase the UX if done correctly.

JSON is a type of data you could get back from an AJAX call.

An example is let's say I have a weather app, and I'm using a weather API on my website. But my website also has a lot of other things going on. I want to refresh the weather data every 60 seconds. Well it would not be good to force a page refresh because I don't want to mess up the user doing other things on the site. So I use AJAX to make a new call every 60 seconds. My call asks the weather API server "How's the weather?" and the server responds. It's very likely the server might respond in a JSON format. Let's say it does. So the server sends me back some JSON format data, and I than have to parse that JSON, and update the values on my website, without a page refresh. That's the difference.

AJAX or JSON are not tied to PHP. PHP is just one of the backend languages that you can use to work with these two things with.

I think you are trying to compare apples with pears.

JSON is a file format. it stands for "JavaScript Object Notation" and it is used to format bits of information. an alternative to JSON would be XML, but XML is much more bloated, so modern applications mostly use JSON nowadays.

AJAX is method for transferring data over the internet. it stands for "asynchronous javascript and xml", but even if it has the "X" in its name for XML, you can also transfer a JSON file asynchronously.

And for PHP... you can use PHP to read or write a JSON-file, which you have previously loaded asynchronously from another server.. but that is not PHP-specific and could be done with another language, too.