Is JSONP safe to use?

Update: JSONP is a common hack to do cross-domain requests. Modern browsers now have Cross Origin Resource Sharing, and IE8+ have XDomainRequest which is similar. See http://enable-cors.org/ for more info. JSONP is just a script include that allows you to use a callback. You should however be aware of Cross-site request forgery (CSRF). As long … Read more

Not a Legal JSONP API — How to get data without CALLBACK parameter

Not a Legal JSONP API The API at that URL is not a legal JSONP API. It can be gotten with a dangerous service: app.service(“dangerousAPI”, function($q) { this.get = get; function get(funcName, url) { var dataDefer = $q.defer(); window[funcName] = function(x) { dataDefer.resolve(x); } var tag = document.createElement(“script”); tag.src = url; document.getElementsByTagName(“head”)[0].appendChild(tag); return dataDefer.promise; } … Read more

ASP.NET MVC 3 JSONP: Does this work with JsonValueProviderFactory?

As far as receiving a JSON string and binding it to a model is concerned the JsonValueProviderFactory does this job out of the box in ASP.NET MVC 3. But there is nothing built-in for outputting JSONP. You could write a custom JsonpResult: public class JsonpResult : JsonResult { public override void ExecuteResult(ControllerContext context) { if … Read more

JSONP call showing “Uncaught SyntaxError: Unexpected token : “

Working fiddle: http://jsfiddle.net/repjt/ $.ajax({ url: ‘https://api.flightstats.com/flex/schedules/rest/v1/jsonp/flight/AA/100/departing/2013/10/4?appId=19d57e69&appKey=e0ea60854c1205af43fd7b1203005d59’, dataType: ‘JSONP’, jsonpCallback: ‘callback’, type: ‘GET’, success: function (data) { console.log(data); } }); I had to manually set the callback to callback, since that’s all the remote service seems to support. I also changed the url to specify that I wanted jsonp.

Angularjs JSONP not working

@TheHippo is correct the data should not just be a plain json response. Here is a working example of a JSONP request against a youtube endpoint in AngularJS. A couple of things to note in this example: Angular’s $http.jsonp converts the request querystring parameter from callback=JSON_CALLBACK to callback=angular.callbacks._0. When calling the youtube endpoint I needed … Read more

How to solve JSON_ERROR_UTF8 error in php json_decode?

There is a good function to sanitize your arrays. I suggest you use a json_encode wrapper like this : function safe_json_encode($value, $options = 0, $depth = 512, $utfErrorFlag = false) { $encoded = json_encode($value, $options, $depth); switch (json_last_error()) { case JSON_ERROR_NONE: return $encoded; case JSON_ERROR_DEPTH: return ‘Maximum stack depth exceeded’; // or trigger_error() or throw … Read more

Use JSONP to load an html page

http://en.wikipedia.org/wiki/JSONP#Script_element_injection Making a JSONP call (in other words, to employ this usage pattern), requires a script element. Therefore, for each new JSONP request, the browser must add (or reuse) a new element—in other words, inject the element—into the HTML DOM, with the desired value for the “src” attribute. This element is then evaluated, the src … Read more