Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSONP and Backbone.js

I would like to use Backbone.js with a REST api I control. I was hoping to have the REST api and the Backbone scripts live on a different domain but unfortunately this will be blocked, as it is a cross domain request.

Does Backbone.js have an built in functionality to support JSONP requests? Or, alternatively, does anyone have any experience with manually adding JSONP support to Backbone.js sync system?

like image 808
Chris W. Avatar asked Aug 24 '11 00:08

Chris W.


People also ask

Is JSONP deprecated?

Yes, JSONP is obsolete now. There's absolutely no reason to offer a JSONP service anymore.

What is JSONP used for?

JSONP is a method for sending JSON data without worrying about cross-domain issues. JSONP does not use the XMLHttpRequest object. JSONP uses the <script> tag instead.

What is JSONP API?

JSONP, or JSON-P (JSON with Padding), is a historical JavaScript technique for requesting data by loading a <script> element, which is an element intended to load ordinary JavaScript.

What is JSONP jquery?

JSONP (which stands for JSON with Padding) builds on this technique and provides us with a way to access the returned data. It does this by having the server return JSON data wrapped in a function call (the “padding”) which can then be interpreted by the browser.


1 Answers

JSONP support for GET operations can be added via fetch's options.

In the same hash where you configure your success and error handlers, add an object like so:

{dataType: "jsonp"}

This will pass along the jsonp option to JQuery's ajax handler, and automagically, you'll have JSONP support for retrieving models / collections.

like image 186
RandallB Avatar answered Sep 22 '22 01:09

RandallB