Contents
Introduction to Cross-Domain AJAX
Cross-Domain AJAX advantages over JSONP
Conclusions
Introduction to Cross-Domain AJAX
Whenever I need to load JavaScript application components from the Web server, I usually insert a JavaScript tag in the Web page document and used some "dirty" tricks to execute it.
Cross-Domain AJAX uses this approach to implement a cleaner solution wrapped as a jQuery plug-in.
Cross-Domain AJAX advantages over JSONP
1. JSONP requires that the server responds with JSON data
JSONP requires that the server returns JSON data wrapped around a padding function to process that data.
Therefore you need to provide the function on the browser side that still needs to process the JSON data to do something useful with it.
Cross-Domain AJAX does not need any JavaScript code to be previously loaded on the browser to process the server response. The JavaScript code that is loaded simply executes by itself, so all the application logic is delivered from the server.
For example, you can start displaying of syndicated videos. If you were searching for content, JSONP cannot do more by itself then delivering the search results, but not retrieve and execute application that actually processes the results.
With Cross-Domain AJAX, loading a complete application, and even additional plugins later, it is not an issue.
In my work with embedded applications, I often need user or site licenses to be verified before loading and executing the application code. Sometimes I need plugins to be loaded on demand. JSONP is not very helpful for these purposes.2. JSONP provides no error handling in case of failure
If for some reason a JSONP fails to respond as expected, there is no way to handle the errors gracefully. With Cross-Domain AJAX, that is not an issue.
If you expect that the response may fail to be returned by the server, you can even set a timeout value, after which you may retry and call the server again. This is easily accomplished by the waiter helper.
Conclusions
This article presents the way I understand the differences between the way JSONP and Cross-Domain AJAX are supposed to work, but I confess that I have not worked much with JSONP, so I admit may be wrong on some aspects.
I rarely need just JSON data. In my applications I mainly need extensions to be loaded dynamically on demand.
My intention with Cross-Domain AJAX was to eliminate the use of iframes from applications like this one. Here all the embedded script does initially is to call an iframe to show the AJAX application. JSONP did not help me on that purpose. With Cross-Domain AJAX, getting rid of the iframes, it is not an issue.