Skip to content Skip to sidebar Skip to footer

Ajax CORS Alternatives

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,

Solution 1:

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!


Solution 2:

The most common solution is indeed using a proxy script on your server.

The bandwidth is most likely negligible - we are talking about small requests, not huge downloads after all.
And the server load is also minimal - and you could always use something asynchronous and lightweight such as node.js to reduce the load even more.


Post a Comment for "Ajax CORS Alternatives"