I know that JSON can be used between 2 web servers, for example facebook API provides some infos using aFile.json so my website need to handle it using PHP’s json_decode() function,
the problem that I can’t imagine any pratical situation wherein we can use it between a server and his client to exchange data. If the client request for some data, the server will use either SQL to retrieve them from a database or a JSON recieved file or whatever else… but finally he will respond only by an HTML file (& probabely some images) so the client’s browser can interpret it,
Am I wrong and the JSON files can be sent to the client ? then why ?
I readed also that JSON is used to communicate between PHP and javascript in the same server, can you give me an example of this case ?
2
Answers
You can think about it in this way:
This second scenario might be used in order to avoid reloading the page which is already render for You.
You can go to developer tools in your browser, you can display section network, and You can observe different HTTP messages which are sent back and forth between client and server.
For more information I suggest reading https://developer.mozilla.org/pl/docs/Web/HTTP because they have nice documentation regarding HTTP there.
JSON is basically a JavaScript Object (JSON stands for JavaScript Object Notation) in text.
You can fetch JSON data from the server with an AJAX call. This is a simplified JavaScript example that makes an AJAX call and then assigns the response JSON to
var myObj
.Servers response
Printing the received data to HTML element “demo” after the response has been assigned to
myObj