{"id":560,"date":"2012-07-08T10:38:39","date_gmt":"2012-07-08T10:38:39","guid":{"rendered":"https:\/\/virtualchaos.co.uk\/blog\/?p=560"},"modified":"2012-07-08T10:50:32","modified_gmt":"2012-07-08T10:50:32","slug":"nodejs-express-with-jsonp-example","status":"publish","type":"post","link":"https:\/\/virtualchaos.co.uk\/blog\/2012\/07\/08\/nodejs-express-with-jsonp-example\/","title":{"rendered":"nodejs + express with jsonp example"},"content":{"rendered":"<p>I&#8217;ve been working more and more with <a href=\"http:\/\/nodejs.org\/\" title=\"nodejs.org\">nodejs<\/a> and have to say I am really loving how easy it&#8217;s been to get to grips with. I&#8217;ll be posting up more about how I&#8217;m using node and the problems I&#8217;m using it to solve over the coming weeks. However I wanted to illustrate just how simple it is do something that would be more work in other languages such as PHP.<\/p>\n<p>So here&#8217;s a quick example I created that illustrates how to make a simple JSONP call to a nodejs + <a href=\"http:\/\/expressjs.com\/\" title=\"ExpressJS\">express<\/a> server.<\/p>\n<p>Here&#8217;s the server side code:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/3070358.js?file=app.js\"><\/script><\/p>\n<p>and here&#8217;s the client side Html code:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/3070390.js?file=test.html\"><\/script><\/p>\n<p>To enable jsonp callback support in an ExpressJS application you just have to include the line:<\/p>\n<blockquote><p>app.enable(&#8220;jsonp callback&#8221;);<\/p><\/blockquote>\n<p>Once you&#8217;ve done this you can use the .json() method on the Response object to handle everything for you. So in my example above any HTTP GET request to \/foo?cb=myfunction would return myfunction(&#8220;hello world&#8221;); with the Content-Type header set to text\/javascript.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working more and more with nodejs and have to say I am really loving how easy it&#8217;s been to get to grips with. I&#8217;ll be posting up more about how I&#8217;m using node and the problems I&#8217;m using &hellip; <a href=\"https:\/\/virtualchaos.co.uk\/blog\/2012\/07\/08\/nodejs-express-with-jsonp-example\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,377],"tags":[404,378,424],"class_list":["post-560","post","type-post","status-publish","format-standard","hentry","category-development","category-nodejs","tag-development","tag-expressjs","tag-nodejs"],"_links":{"self":[{"href":"https:\/\/virtualchaos.co.uk\/blog\/wp-json\/wp\/v2\/posts\/560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/virtualchaos.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/virtualchaos.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/virtualchaos.co.uk\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/virtualchaos.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=560"}],"version-history":[{"count":0,"href":"https:\/\/virtualchaos.co.uk\/blog\/wp-json\/wp\/v2\/posts\/560\/revisions"}],"wp:attachment":[{"href":"https:\/\/virtualchaos.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/virtualchaos.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/virtualchaos.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}