{"id":330,"date":"2011-02-09T20:21:09","date_gmt":"2011-02-10T01:21:09","guid":{"rendered":"http:\/\/osric.com\/chris\/accidental-developer\/?p=330"},"modified":"2011-02-09T20:21:09","modified_gmt":"2011-02-10T01:21:09","slug":"jsonp-and-sencha-touch","status":"publish","type":"post","link":"https:\/\/osric.com\/chris\/accidental-developer\/2011\/02\/jsonp-and-sencha-touch\/","title":{"rendered":"JSONP and Sencha Touch"},"content":{"rendered":"<p>I was recently trying to get a <a href=\"http:\/\/www.sencha.com\/products\/touch\/\">Sencha Touch<\/a> demo up-and-running, but my callback functions after requests for JSON data never ran, and Firefox would throw errors along the lines of &#8220;invalid label.&#8221; I didn&#8217;t understand why&#8211;until I read more about <a href=\"http:\/\/en.wikipedia.org\/wiki\/JSON#JSONP\">JSONP<\/a>.<\/p>\n<p>JSONP prefixes your JSON response with a function name, which runs when the response is retrieved. It&#8217;s a way of handling the data without a listener.<\/p>\n<p>This means that your JSONP provider needs to detect a JSONP parameter, and then wrap or &#8220;pad&#8221; the response within the specified parameter value.<\/p>\n<p>For Sencha Touch, the JSON returned should be wrapped in <em>Ext.util.JSONP.callback();<\/em>. If your JSON looks like this:<\/p>\n<p><code>{\"results\":[{\"name\":\"Chris\"},{\"name\",\"Harry\"}]}<\/code><\/p>\n<p>then your JSONP should look like this:<\/p>\n<p><code>Ext.util.JSONP.callback({\"results\":[{\"name\":\"Chris\"},{\"name\",\"Harry\"}]});<\/code><\/p>\n<p>Not that you should hard-code that function name anywhere in your JSON output&#8211;web-based APIs and services should pick up the function from your request and wrap the JSON for you. For example, the <a href=\"http:\/\/apiwiki.twitter.com\/w\/page\/22554756\/Twitter-Search-API-Method:-search\">twitter search API<\/a> accepts a <em>callback<\/em> querystring parameter.<\/p>\n<p><code>http:\/\/search.twitter.com\/search.json?q=fakecriterions&amp;callback=myCallbackFunction<\/code><\/p>\n<p>would wrap the JSON response inside<\/p>\n<p><code>myCallbackFunction();<\/code><\/p>\n<p>I ignored the extra letter in the acronym at my own peril&#8211;I figured it was just a trivial variation on JSON (which it is) that wouldn&#8217;t make any difference in how it was handled (but it does).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was recently trying to get a Sencha Touch demo up-and-running, but my callback functions after requests for JSON data never ran, and Firefox would throw errors along the lines of &#8220;invalid label.&#8221; I didn&#8217;t understand why&#8211;until I read more about JSONP. JSONP prefixes your JSON response with a function name, which runs when the &hellip; <a href=\"https:\/\/osric.com\/chris\/accidental-developer\/2011\/02\/jsonp-and-sencha-touch\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">JSONP and Sencha Touch<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[168,167,350,161,162,163,354],"class_list":["post-330","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-api","tag-callback","tag-javascript","tag-json","tag-jsonp","tag-sencha-touch","tag-twitter"],"_links":{"self":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/330","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/comments?post=330"}],"version-history":[{"count":2,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/330\/revisions"}],"predecessor-version":[{"id":339,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/330\/revisions\/339"}],"wp:attachment":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/media?parent=330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/categories?post=330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/tags?post=330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}