JSONP和Backbone.js的 [英] JSONP and Backbone.js

查看:110
本文介绍了JSONP和Backbone.js的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Backbone.js的一个REST API我控制。我希望能有REST API和脚本骨干住在不同的领域,但不幸的是这会被阻止,因为它是一个跨域请求。

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.

Backbone.js的是否有一个内置的功能,支持JSONP请求?或者,没有任何人有手动添加JSONP支持Backbone.js的任何经验同步系统?

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?

推荐答案

您将无法使用您的整个REST API与JSONP。你只能调用get与JSONP请求(它的工作原理是写一个新的<脚本> 当前文档,然后调用JavaScript回调...标记)。

You will not be able to use your entire REST API with JSONP. You can only call GET requests with JSONP (it works by writing a new <script> tag on the current document, then calling a javascript callback...).

要使用所有的HTTP动词(POST,DELETE,PUT),您可以使用CORS协议:的http:// www.w3.org/TR/access-control/

To use all HTTP verb (POST, DELETE, PUT), you can use the CORS protocol : http://www.w3.org/TR/access-control/.

CORS是一个浏览器和Web服务,告诉浏览器,这是OK从跨域调用执行JavaScript code之间协商的协议

CORS is a protocol negotiated between a browser and a web-service that tells the browser that it is "OK" to execute Javascript code from a cross-domain call

要使用此功能,你只需要在告诉它的确定接受跨域请求您的浏览器服务器响应一些自定义头。这里有一个博客文章,介绍如何使用RubyOnRails实现它(但它应该是与其他框架...颇为相似): http://www.tsheffler.com/blog/?p=428

To use this, you just need to include some custom headers in your server response that tells the browser that it's ok to accept cross domain requests. Here's an blog post that explains how to implement it with RubyOnRails (but it should be quite similar with others framework...) : http://www.tsheffler.com/blog/?p=428

这是最简单的解决方案,可以使用Backbone.js的,如果你在哪里在同一个域,并与最新的浏览器(Internet Explorer 8以上版本,火狐3.5 +,Safari浏览器4+和Chrome)的作品!

It's the simplest solution, you can use backbone.js as if you where on the same domain, and it works with most current browsers (Internet Explorer 8+, Firefox 3.5+, Safari 4+, and Chrome) !

如果您需要旧的浏览器的支持,我没有管理使用,使骨干工作 easyXDM

If you need older browser support, I did manage to make backbone work using easyXDM :

easyXDM是一个JavaScript库,使您作为一个开发人员能够轻松地解决了同源策略设置到位的限制,进而使其易于沟通和跨域边界暴露的JavaScript API的。

easyXDM is a Javascript library that enables you as a developer to easily work around the limitation set in place by the Same Origin Policy, in turn making it easy to communicate and expose javascript API's across domain boundaries.

这是一个有点复杂,并与一些知名的黑客IFRAME(即有时在JavaScript中的小部件一样GMaps实现,Facebook的小部件使用,...)的作品。

It's a little more complicated, and works with a some well known iframe hacks (that are sometimes used in javascript widgets like GMaps, facebook widgets, ...).

希望这有助于!

这篇关于JSONP和Backbone.js的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆