阿贾克斯CORS替代品 [英] Ajax CORS alternatives

查看:146
本文介绍了阿贾克斯CORS替代品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在域api.example.com有一个REST API服务。而想要使用这个API也为我的JavaScript在www.example.com Ajax请求。

我建立我的jQuery Ajax请求(GET,POST,PUT和DELETE)采用CORS,并在Firefox和Chrome一切工作正常。我测试脚本的戏并没有什么装。在此之后,我看了一些网上的文章,并必须认识到,CORS不能与Opera一起。

什么选择有获取和放置数据的REST服务? JSONP不工作,因为我需要其他HTTP动词(POST,PUT,DELETE)了。

创建一个隧道在同一个域中的PHP脚本?但比,我有更多的网络流量和Serverload。

 客户端 - > WWW-服务器 - > API-服务器
API  - 服务器 - > WWW-服务器 - >客户
 

而不是

 客户端 - > API-服务器
API  - 服务器 - >客户
 

我很感激地对每一个想法,将与我共享。

解决方案

您可以使用第三方工具,隧道,如的 YQL

  

雅虎查询语言是一个前pressive类似于SQL的语言,可以让你查​​询,过滤器,并加入数据通过Web服务。

YQL将作为中间人来连接从您的网站的API调用。网络流量是这样的:

 客户端 - > YQL  - > API服务器
 

这看起来好像没什么问题。

YQL已经存在了相当长的时间。他们免费配额有利于和正常运行时间为pretty的好,太:

  
      
  • 每IP限制:/ V1 /公/ :每小时2000调用; / V1 / YQL / 的:每小时20000电话

  • 。   
  • YQL有超过99.5%的性能的正常运行时间的目标。

  •   

此外,配额的不断增加和雅虎提供了一定的保证,如果他们决定关闭该服务,他们会保持运行相当长一段时间,所以你将有时间进行迁移。有迹象表明,使用YQL今天几个项目(我知道一个这样的服务 - TipTheWb.org

另外,我建议你来检测一个给定的浏览器是否支持CORS,并退回到YQL只有在必要的:

 函数browserSupportsCors(){
    如果(在新XMLHtt prequestwithCredentials())
        返回true; //大多数浏览器
    否则,如果(typeof运算XDomainRequest ==对象)
        返回true; // IE8 +
    否则//歌剧目前在这里,但他们很快就会好pretty的:)
    返回false;
}
 

如果你使用jQuery它有一个整洁的 支持 方法。

 如果(!$。support.cors){
    // YQL回退
}
 

也有很大的技巧来调整YQL效果


谢谢你关心Opera用户!

I have an REST API Service on domain api.example.com. And want to use this api also for my javascript ajax Requests on www.example.com.

I build my jQuery AJAX Requests (GET, POST, PUT and DELETE) using CORS, and on firefox and chrome all works fine. I tested the script on Opera and nothing loaded. After this, I read some online Articles and have to realize that CORS not work with Opera.

What alternatives there are to Get and Put Data to the Rest Service? JSONp don't work, because I need the other HTTP Verbs (POST, PUT, DELETE) too.

Create an "tunneling" php-Script on same domain? But than, I have much more Network traffic and Serverload.

Client -> WWW-Server -> API-Server
API-Server -> WWW-Server -> Client

instead of

Client -> API-Server
API-Server -> Client

I'm thankfully for every Idea which will be shared with me.

解决方案

You can use third-party tools for tunneling, for example YQL.

The Yahoo! Query Language is an expressive SQL-like language that lets you query, filter, and join data across Web services.

YQL would act as a middleman to wire up calls from your site to your API. The network traffic would look like this:

Client -> YQL -> API Server

which looks fine to me.

YQL has been around for quite a long time. Their free quotas are favorable and uptime is pretty good, too:

  • Per IP limits: /v1/public/: 2,000 calls per hour; /v1/yql/: 20,000 calls per hour.

  • YQL has a performance uptime target of over 99.5%.

Moreover, quotas are constantly increasing and Yahoo! provides certain guarantees that if they decide to shutdown the service they'll keep it running for quite a while so you'll have time to migrate. There are several projects that use YQL today (I'm aware of one such service - TipTheWb.org)

In addition I suggest you to detect whether a given browser supports CORS and fallback to YQL only if necessary:

function browserSupportsCors() {
    if ("withCredentials" in new XMLHttpRequest())
        return true; // most browsers
    else if (typeof XDomainRequest == "object")
        return true; // IE8+
    else // Opera currently here, but they'll get better pretty soon :)
    return false;
}

or if you use jQuery it has a neat support method.

if (!$.support.cors) {
    // YQL fallback
}

There are also great tips to tweak YQL performance.


Thank you for caring about Opera users!

这篇关于阿贾克斯CORS替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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