JSON string to JS object

Some modern browsers have support for parsing JSON into a native object: var var1 = ‘{“cols”: [{“i” ……. 66}]}’; var result = JSON.parse(var1); For the browsers that don’t support it, you can download json2.js from json.org for safe parsing of a JSON object. The script will check for native JSON support and if it doesn’t … 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

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

Callback function for JSONP with jQuery AJAX

This is what I do on mine $(document).ready(function() { if ($(‘#userForm’).valid()) { var formData = $(“#userForm”).serializeArray(); $.ajax({ url: ‘http://www.example.com/user/’ + $(‘#Id’).val() + ‘?callback=?’, type: “GET”, data: formData, dataType: “jsonp”, jsonpCallback: “localJsonpCallback” }); }); function localJsonpCallback(json) { if (!json.Error) { $(‘#resultForm’).submit(); } else { $(‘#loading’).hide(); $(‘#userForm’).show(); alert(json.Message); } }

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.

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

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