{"id":11,"date":"2015-03-23T12:49:53","date_gmt":"2015-03-23T12:49:53","guid":{"rendered":"http:\/\/galtalmor.com\/blog\/?p=11"},"modified":"2015-04-17T16:25:57","modified_gmt":"2015-04-17T14:25:57","slug":"custom-angularjs-jsonp-callback-name","status":"publish","type":"post","link":"https:\/\/galtalmor.com\/blog\/?p=11","title":{"rendered":"Custom AngularJS JSONP callback name"},"content":{"rendered":"<p>At my work place we are working on some AngularJS projects. We also write unit tests using Jasmine over Karma.<\/p>\n<p>So today I found out a colleague at work used jQuery&#8217;s $.getJSON and not Angular&#8217;s $http, since he had to make a JSONP request to a service which did not accept function names with dot (.) sign. Apparently Angular is calling its callback functions in JSONP requests in the following structure:\u00a0angular.callbacks._{id}, which did not work well with the service we tried to use. This solution prevented us from being able to test the call using Angular&#8217;s injection mechanism.<\/p>\n<p>I thought to myself there must be a way to tell Angular which callback name structure to use, since I wanted to be able to do it the Angular way and not use jQuery unless\u00a0it was the only option.<\/p>\n<p>Of course I turned to Google and started searching for a solution and here&#8217;s what I found up. It seems that the default name is hard coded in Angular&#8217;s code, but eventually I stumbled across <a href=\"http:\/\/stackoverflow.com\/questions\/25400891\/how-to-custom-set-angularjs-jsonp-callback-name\">this stackoverflow question<\/a> which held\u00a0exactly the answer to my problem.<\/p>\n<p>The idea is to register an\u00a0interceptor in our Angular app config, that all requests done using Angular&#8217;s $httpProvider will pass through. In this\u00a0interceptor we will check if the request type is JSONP and if so we will replace the callback in the URL (the string\u00a0JSON_CALLBACK) to whatever we want, instead of the original replacement that Angular is doing. In our code example it is <code>'angular_callbacks_' + callbackId<\/code> .<\/p>\n<p>I&#8217;ll spare you the time and send you straight to the code written in the answer by\u00a0<a href=\"http:\/\/stackoverflow.com\/users\/867480\/runtarm\">runTarm<\/a> on the <a href=\"http:\/\/stackoverflow.com\/questions\/25400891\/how-to-custom-set-angularjs-jsonp-callback-name\">question page<\/a>, with the example found on <a href=\"http:\/\/plnkr.co\/edit\/S5K46izpIxHat3gLqvu7?p=preview\">this plnkr page<\/a>:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar app = angular.module('myApp', &#x5B;])\r\n  .config(function($httpProvider) {\r\n    $httpProvider.interceptors.push('jsonpInterceptor');\r\n  })\r\n\r\n  .controller('MainCtrl', function($scope, $http) {\r\n    $http.jsonp('http:\/\/someservice.com?callback=JSON_CALLBACK')\r\n      .then(function(response) {\r\n        console.log(response.data);\r\n      });\r\n  })\r\n\r\n  .factory('jsonpInterceptor', function($timeout, $window) {\r\n    return {\r\n      'request': function(config) {\r\n        if (config.method === 'JSONP') {\r\n          var callbackId = angular.callbacks.counter.toString(36);\r\n          config.callbackName = 'angular_callbacks_' + callbackId;\r\n          config.url = config.url.replace('JSON_CALLBACK', config.callbackName);\r\n\r\n          $timeout(function() {\r\n            $window&#x5B;config.callbackName] = angular.callbacks&#x5B;'_' + callbackId];\r\n          }, 0, false);\r\n        }\r\n\r\n        return config;\r\n      },\r\n\r\n      'response': function(response) {\r\n        var config = response.config;\r\n        if (config.method === 'JSONP') {\r\n          delete $window&#x5B;config.callbackName]; \/\/ cleanup\r\n        }\r\n\r\n        return response;\r\n      },\r\n\r\n      'responseError': function(rejection) {\r\n        var config = rejection.config;\r\n        if (config.method === 'JSONP') {\r\n          delete $window&#x5B;config.callbackName]; \/\/ cleanup\r\n        }\r\n\r\n        return $q.reject(rejection);\r\n      }\r\n    };\r\n  });\r\n<\/pre>\n<p>I hope this helps you as it helped me.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At my work place we are working on some AngularJS projects. We also write unit tests using Jasmine over Karma. So today I found out a colleague at work used jQuery&#8217;s $.getJSON and not Angular&#8217;s $http, since he had to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":36,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[4,6,5],"class_list":["post-11","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-code","tag-angularjs","tag-code","tag-javascript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Custom AngularJS JSONP callback name - Gal&#039;s Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/galtalmor.com\/blog\/?p=11\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Custom AngularJS JSONP callback name - Gal&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"At my work place we are working on some AngularJS projects. We also write unit tests using Jasmine over Karma. So today I found out a colleague at work used jQuery&#8217;s $.getJSON and not Angular&#8217;s $http, since he had to...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/galtalmor.com\/blog\/?p=11\" \/>\n<meta property=\"og:site_name\" content=\"Gal&#039;s Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/galtalmor\" \/>\n<meta property=\"article:published_time\" content=\"2015-03-23T12:49:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-04-17T14:25:57+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/galtalmor.com\/blog\/wp-content\/uploads\/2015\/03\/jsonp.png\" \/>\n\t<meta property=\"og:image:width\" content=\"823\" \/>\n\t<meta property=\"og:image:height\" content=\"400\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"galtalmor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@galtalmor\" \/>\n<meta name=\"twitter:site\" content=\"@galtalmor\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"galtalmor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/galtalmor.com\/blog\/?p=11#article\",\"isPartOf\":{\"@id\":\"https:\/\/galtalmor.com\/blog\/?p=11\"},\"author\":{\"name\":\"galtalmor\",\"@id\":\"https:\/\/galtalmor.com\/blog\/#\/schema\/person\/ba0304e98667aa0165abe7c766780b3d\"},\"headline\":\"Custom AngularJS JSONP callback name\",\"datePublished\":\"2015-03-23T12:49:53+00:00\",\"dateModified\":\"2015-04-17T14:25:57+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/galtalmor.com\/blog\/?p=11\"},\"wordCount\":419,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\/\/galtalmor.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/galtalmor.com\/blog\/?p=11#primaryimage\"},\"thumbnailUrl\":\"https:\/\/galtalmor.com\/blog\/wp-content\/uploads\/2015\/03\/jsonp.png\",\"keywords\":[\"AngularJS\",\"Code\",\"JavaScript\"],\"articleSection\":[\"Code\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/galtalmor.com\/blog\/?p=11#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/galtalmor.com\/blog\/?p=11\",\"url\":\"https:\/\/galtalmor.com\/blog\/?p=11\",\"name\":\"Custom AngularJS JSONP callback name - Gal&#039;s Blog\",\"isPartOf\":{\"@id\":\"https:\/\/galtalmor.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/galtalmor.com\/blog\/?p=11#primaryimage\"},\"image\":{\"@id\":\"https:\/\/galtalmor.com\/blog\/?p=11#primaryimage\"},\"thumbnailUrl\":\"https:\/\/galtalmor.com\/blog\/wp-content\/uploads\/2015\/03\/jsonp.png\",\"datePublished\":\"2015-03-23T12:49:53+00:00\",\"dateModified\":\"2015-04-17T14:25:57+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/galtalmor.com\/blog\/?p=11#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/galtalmor.com\/blog\/?p=11\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/galtalmor.com\/blog\/?p=11#primaryimage\",\"url\":\"https:\/\/galtalmor.com\/blog\/wp-content\/uploads\/2015\/03\/jsonp.png\",\"contentUrl\":\"https:\/\/galtalmor.com\/blog\/wp-content\/uploads\/2015\/03\/jsonp.png\",\"width\":823,\"height\":400},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/galtalmor.com\/blog\/?p=11#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/galtalmor.com\/blog\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Custom AngularJS JSONP callback name\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/galtalmor.com\/blog\/#website\",\"url\":\"https:\/\/galtalmor.com\/blog\/\",\"name\":\"Gal&#039;s Blog\",\"description\":\"Web developer, gamer, DJ and a computer geek in general.\",\"publisher\":{\"@id\":\"https:\/\/galtalmor.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/galtalmor.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/galtalmor.com\/blog\/#organization\",\"name\":\"Gal's Blog\",\"url\":\"https:\/\/galtalmor.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/galtalmor.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/galtalmor.com\/blog\/wp-content\/uploads\/2015\/03\/860943_10151310903518946_1561965836_o-1.jpg\",\"contentUrl\":\"http:\/\/galtalmor.com\/blog\/wp-content\/uploads\/2015\/03\/860943_10151310903518946_1561965836_o-1.jpg\",\"width\":1354,\"height\":900,\"caption\":\"Gal's Blog\"},\"image\":{\"@id\":\"https:\/\/galtalmor.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/galtalmor\",\"https:\/\/x.com\/galtalmor\",\"https:\/\/www.linkedin.com\/in\/galtalmor\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/galtalmor.com\/blog\/#\/schema\/person\/ba0304e98667aa0165abe7c766780b3d\",\"name\":\"galtalmor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/galtalmor.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/f2ff78ce23611579e3eafe9c98a2a277?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/f2ff78ce23611579e3eafe9c98a2a277?s=96&d=mm&r=g\",\"caption\":\"galtalmor\"},\"url\":\"https:\/\/galtalmor.com\/blog\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Custom AngularJS JSONP callback name - Gal&#039;s Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/galtalmor.com\/blog\/?p=11","og_locale":"en_US","og_type":"article","og_title":"Custom AngularJS JSONP callback name - Gal&#039;s Blog","og_description":"At my work place we are working on some AngularJS projects. We also write unit tests using Jasmine over Karma. So today I found out a colleague at work used jQuery&#8217;s $.getJSON and not Angular&#8217;s $http, since he had to...","og_url":"https:\/\/galtalmor.com\/blog\/?p=11","og_site_name":"Gal&#039;s Blog","article_publisher":"https:\/\/www.facebook.com\/galtalmor","article_published_time":"2015-03-23T12:49:53+00:00","article_modified_time":"2015-04-17T14:25:57+00:00","og_image":[{"width":823,"height":400,"url":"https:\/\/galtalmor.com\/blog\/wp-content\/uploads\/2015\/03\/jsonp.png","type":"image\/png"}],"author":"galtalmor","twitter_card":"summary_large_image","twitter_creator":"@galtalmor","twitter_site":"@galtalmor","twitter_misc":{"Written by":"galtalmor","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/galtalmor.com\/blog\/?p=11#article","isPartOf":{"@id":"https:\/\/galtalmor.com\/blog\/?p=11"},"author":{"name":"galtalmor","@id":"https:\/\/galtalmor.com\/blog\/#\/schema\/person\/ba0304e98667aa0165abe7c766780b3d"},"headline":"Custom AngularJS JSONP callback name","datePublished":"2015-03-23T12:49:53+00:00","dateModified":"2015-04-17T14:25:57+00:00","mainEntityOfPage":{"@id":"https:\/\/galtalmor.com\/blog\/?p=11"},"wordCount":419,"commentCount":2,"publisher":{"@id":"https:\/\/galtalmor.com\/blog\/#organization"},"image":{"@id":"https:\/\/galtalmor.com\/blog\/?p=11#primaryimage"},"thumbnailUrl":"https:\/\/galtalmor.com\/blog\/wp-content\/uploads\/2015\/03\/jsonp.png","keywords":["AngularJS","Code","JavaScript"],"articleSection":["Code"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/galtalmor.com\/blog\/?p=11#respond"]}]},{"@type":"WebPage","@id":"https:\/\/galtalmor.com\/blog\/?p=11","url":"https:\/\/galtalmor.com\/blog\/?p=11","name":"Custom AngularJS JSONP callback name - Gal&#039;s Blog","isPartOf":{"@id":"https:\/\/galtalmor.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/galtalmor.com\/blog\/?p=11#primaryimage"},"image":{"@id":"https:\/\/galtalmor.com\/blog\/?p=11#primaryimage"},"thumbnailUrl":"https:\/\/galtalmor.com\/blog\/wp-content\/uploads\/2015\/03\/jsonp.png","datePublished":"2015-03-23T12:49:53+00:00","dateModified":"2015-04-17T14:25:57+00:00","breadcrumb":{"@id":"https:\/\/galtalmor.com\/blog\/?p=11#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/galtalmor.com\/blog\/?p=11"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/galtalmor.com\/blog\/?p=11#primaryimage","url":"https:\/\/galtalmor.com\/blog\/wp-content\/uploads\/2015\/03\/jsonp.png","contentUrl":"https:\/\/galtalmor.com\/blog\/wp-content\/uploads\/2015\/03\/jsonp.png","width":823,"height":400},{"@type":"BreadcrumbList","@id":"https:\/\/galtalmor.com\/blog\/?p=11#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/galtalmor.com\/blog"},{"@type":"ListItem","position":2,"name":"Custom AngularJS JSONP callback name"}]},{"@type":"WebSite","@id":"https:\/\/galtalmor.com\/blog\/#website","url":"https:\/\/galtalmor.com\/blog\/","name":"Gal&#039;s Blog","description":"Web developer, gamer, DJ and a computer geek in general.","publisher":{"@id":"https:\/\/galtalmor.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/galtalmor.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/galtalmor.com\/blog\/#organization","name":"Gal's Blog","url":"https:\/\/galtalmor.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/galtalmor.com\/blog\/#\/schema\/logo\/image\/","url":"http:\/\/galtalmor.com\/blog\/wp-content\/uploads\/2015\/03\/860943_10151310903518946_1561965836_o-1.jpg","contentUrl":"http:\/\/galtalmor.com\/blog\/wp-content\/uploads\/2015\/03\/860943_10151310903518946_1561965836_o-1.jpg","width":1354,"height":900,"caption":"Gal's Blog"},"image":{"@id":"https:\/\/galtalmor.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/galtalmor","https:\/\/x.com\/galtalmor","https:\/\/www.linkedin.com\/in\/galtalmor"]},{"@type":"Person","@id":"https:\/\/galtalmor.com\/blog\/#\/schema\/person\/ba0304e98667aa0165abe7c766780b3d","name":"galtalmor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/galtalmor.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/f2ff78ce23611579e3eafe9c98a2a277?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f2ff78ce23611579e3eafe9c98a2a277?s=96&d=mm&r=g","caption":"galtalmor"},"url":"https:\/\/galtalmor.com\/blog\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/galtalmor.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/11"}],"collection":[{"href":"https:\/\/galtalmor.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/galtalmor.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/galtalmor.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/galtalmor.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11"}],"version-history":[{"count":5,"href":"https:\/\/galtalmor.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/11\/revisions"}],"predecessor-version":[{"id":21,"href":"https:\/\/galtalmor.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/11\/revisions\/21"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/galtalmor.com\/blog\/index.php?rest_route=\/wp\/v2\/media\/36"}],"wp:attachment":[{"href":"https:\/\/galtalmor.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/galtalmor.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/galtalmor.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}