Just a quick note for how to modify a standard JSON reply to support requesting data across domains
The answer here is really quite simple …if the &callback=something parameter is provided, then:
//JSONP opening
if ( callback != null )
jsonBuf.append ( callback + "(" );
//Beginning of standard reply
JSON data
//End of standard reply
//JSONP closure
if ( callback != null )
jsonBuf.append ( ");" );
If you’re like most people, and JSON-P is being considered long after all of your REST end-points have already been created, then coding a method that simply wraps the existing REST replies within the “something( normal rest reply );” construct is a very fast way of supporting cross-browser scripting for rest clients.
- It appears there’s a performance penalty for using JSON-P instead of straight JSON replies. See http://jsperf.com/ajax-jsonp-vs-ajax-json for more information.
- Just FYI, Jersey already supports JSON-P with a simple annotation of the REST end-points, and a minor wrapping of the reply as noted above. Further, Jersey recognizes the application/x-javascript mine type to return a JSON-P response without requiring the &callback= query parameter.
- JSON-P is a mature enough technology that JQuery, Dojo, YUI, MooTools, Windows WCF, Sencha Touch, IBM BPM, and most other relevant web client and server products now support it.
- Our chosen DataTables also supports JSON-P with the following modification:
"fnServerData": function( sUrl, aoData, fnCallback, oSettings ) { oSettings.jqXHR = $.ajax( { "url" : sUrl, "data" : aoData, "dataType" : "jsonp", "cache" : false, "success" : fnCallback } ); }