<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>My Blog &#187; Everything Else</title>
	<atom:link href="http://www.peterclemons.org/blog/?cat=1&#038;feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.peterclemons.org/blog</link>
	<description>Software Engineering Topics</description>
	<lastBuildDate>Wed, 08 Aug 2012 15:55:30 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.9.40</generator>
	<item>
		<title>How to support JSON-P on the server</title>
		<link>http://www.peterclemons.org/blog/?p=251</link>
		<comments>http://www.peterclemons.org/blog/?p=251#comments</comments>
		<pubDate>Tue, 31 Jul 2012 22:32:59 +0000</pubDate>
		<dc:creator><![CDATA[peter]]></dc:creator>
				<category><![CDATA[Everything Else]]></category>

		<guid isPermaLink="false">http://www.peterclemons.org/blog/?p=251</guid>
		<description><![CDATA[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 &#8230;if the &#038;callback=something parameter is provided, then: //JSONP opening if ( callback != null ) &#8230; <a href="http://www.peterclemons.org/blog/?p=251">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Just a quick note for how to modify a standard JSON reply to support requesting data across domains</p>
<p>The answer here is really quite simple &#8230;if the &#038;callback=something parameter is provided, then:</p>
<pre>
    //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 ( ");" );
</pre>
<p>If you&#8217;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 &#8220;something( normal rest reply );&#8221; construct is a very fast way of supporting cross-browser scripting for rest clients.</p>
<ul>
<li>It appears there&#8217;s a performance penalty for using JSON-P instead of straight JSON replies.  See <a href="http://jsperf.com/ajax-jsonp-vs-ajax-json">http://jsperf.com/ajax-jsonp-vs-ajax-json</a> for more information.
<li>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 &#038;callback= query parameter.
<li>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.
<li>Our chosen DataTables also supports JSON-P with the following modification:
<pre>
"fnServerData": function( sUrl, aoData, fnCallback, oSettings )
{
   oSettings.jqXHR = $.ajax(
   {
      "url"       : sUrl,
      "data"      : aoData,
      "dataType"  : "jsonp",
      "cache"     : false,
      "success"   : fnCallback
   } );
}
</pre>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.peterclemons.org/blog/?feed=rss2&#038;p=251</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
