1.4 KiB
1.4 KiB
title |
---|
Learn About Jsonp |
JSONP
JSONP stands for "JSON with padding". Let's say you want to make AJAX requests to a different domain. Well, you can't do this with XMLHttpRequest, as you normally would, but you CAN do this with script tags, as seen on StackOverflow:
script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://www.someWebApiServer.com/some-data';
But this is ugly, now we have to get elements of a JSON out of a script tag, gross. Luckily, the creators of JSONP were thinking ahead, so instead of setting our scripts as we did above, we do this:
script.src = 'http://www.someWebApiServer.com/some-data?callback=my_callback';
This triggers an automatic callback after the data has loaded, creating a function with the data desired inside of it.