Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between JSON and JSONP? [duplicate]

Possible Duplicate:
Please explain JSONP

For example in the jQuery documentation I find both JSON and JSONP mentioned. What is the difference exactly? How can I see which is which? Which one should be used for what?

And what does the PHP function json_encode generate?

like image 214
Svish Avatar asked Jun 13 '10 16:06

Svish


1 Answers

JSON is a simple data format. JSONP is a methodology for using that format with cross-domain ajax requests while not being hit by Same Origin Policy issues. Basically, the idea is that instead of using ajax to request JSON-encoded data, you add a script tag to your page that loads the data as a JavaScript script and makes a callback to your code saying "Here's the data." This works because the "origin" applied to JavaScript scripts is the origin of the document, not where the script came from, which means it can access your code in order to call the callback.

json_encode produces JSON. You might use json_encode as part of providing a JSONP interface to your system, if you need to enable cross-domain calls.

See also CORS, which may increasingly be used for this instead as we go forward, but which isn't yet supported well in IE (IE7 and below don't have it at all; IE8 has it but requires that the client-side code do special things; Chrome, Firefox, and the like have it and don't require the client-side code to do anything special).

like image 140
T.J. Crowder Avatar answered Oct 21 '22 08:10

T.J. Crowder