<?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>In Nomine - The Lotus Land &#187; json</title>
	<atom:link href="http://www.in-nomine.org/tag/json/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.in-nomine.org</link>
	<description>The focused mind can pierce through stone...</description>
	<lastBuildDate>Sat, 14 May 2011 20:22:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>JSONP with Werkzeug</title>
		<link>http://www.in-nomine.org/2009/04/03/jsonp-with-werkzeug/</link>
		<comments>http://www.in-nomine.org/2009/04/03/jsonp-with-werkzeug/#comments</comments>
		<pubDate>Fri, 03 Apr 2009 13:07:15 +0000</pubDate>
		<dc:creator>asmodai</dc:creator>
				<category><![CDATA[Browsers]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[jsonp]]></category>
		<category><![CDATA[werkzeug]]></category>

		<guid isPermaLink="false">http://www.in-nomine.org/?p=269</guid>
		<description><![CDATA[So I had implemented a simple JSON data server with Werkzeug for a classroom experiment. Unfortunately in my haste to get everything up and running I totally forgot about the fact that, since we cannot allow uploads to this server &#8230; <a href="http://www.in-nomine.org/2009/04/03/jsonp-with-werkzeug/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>So I had implemented a simple <a title="JSON data format website" href="http://www.json.org/">JSON</a> data server with Werkzeug for a classroom experiment. Unfortunately in my haste to get everything up and running I totally forgot about the fact that, since we cannot allow uploads to this server of various custom made webpages, using <a title="Ajax/jQuery.ajax() documentation" href="http://docs.jquery.com/Ajax/jQuery.ajax">jQuery&#8217;s <code>$.ajax()</code></a> everything just fails since it will then be a cross-site scripting request.</p>
<p>So, normally you would do something like the following in order to return JSON data:</p>
<pre lang="python">return json.dumps(data)</pre>
<p>Which would be used with the $.ajax() call in a way like the following:</p>
<pre lang="javascript">$.ajax({
  type: "POST",
  url: "http://example.com/json/something",
  data: "parameter=value",
  dataType: "json",
  error: function(XMLHttpRequest, textStatus, errorThrown){},
  success: function(data, msg){}
});</pre>
<p>Which is perfectly fine for scripts getting and using the data on the same host/domain. But, as said before, this will fail with warnings similar to: <code>"Access to restricted URI denied" code: "1012" nsresult: "0xdeadc0de (NS_ERROR_DOM_BAD_URI)"</code>.</p>
<p>One way out of this is using <a title="Remote JSON - JSONP" href="http://bob.pythonmac.org/archives/2005/12/05/remote-json-jsonp/">JSONP</a>. jQuery has a <a title="Ajax/jQuery.getJSON() documentation" href="http://docs.jquery.com/Ajax/jQuery.getJSON"><code>$.getJSON()</code> function</a>, which loads JSON data using a HTTP GET request. Now, the simplistic way to convert your code would be to change it as such:</p>
<pre lang="javascript">$.getJSON("http://example.com/json/something",
  function(data){}
);</pre>
<p>But this causes another issue. Since <code>$.getJSON()</code> GETs the JSON data, but doesn&#8217;t use eval() on it, but instead pulls the result into <code>script</code> tags, it somehow causes,on Firefox at least, an invalid label error. In order to fix this you need to set up the JSON data server to properly support a callback argument, to use <code>$.getJSON()</code> how it is meant to be used:</p>
<pre lang="javascript">$.getJSON("http://example.com/json/something?jsoncallback=?",
  function(data){}
);</pre>
<p>In the code above the additional parameter <code>jsoncallback</code> will, thanks to jQuery, get the question mark replaced by an alphanumeric string (typically in the form of <code>jsonp</code> followed by a timestamp). This value should be used to wrap the resulting JSON data with. This means you would have to change the initial Python code to something like this:</p>
<pre lang="python">return request.args.get('jsoncallback') + '(' + json.dumps(data) + ')'</pre>
<p>Of course this causes problems when you want to reuse the code for both AJAX use on the same host/domain and use it from outside. So in order to make both work you can test on whether or not the callback parameter is available and return the appropriate data. I came up with this little snippet for that:</p>
<pre lang="python">def jsonwrapper(self, request, data):
    callback = request.args.get('jsoncallback')

    if callback:
        return callback + '(' + json.dumps(data) + ')'
    else:
        return json.dumps(data)</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.in-nomine.org/2009/04/03/jsonp-with-werkzeug/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

