[English] Intel AppUp(TM) Encapsulator 2.0, XMLHttpRequests & CORS

04. Juni 2012 von Rene Hennig

This article assumes knowledge of JavaScript, jQuery, and Intel AppUp (TM) Encapsulator.

Intel AppUpThe new encapsulator version brings many new HTML5 and CSS3 features. There are some good reasons to upgrade an existing app from encapsulator version 1.0 to 2.0. Unfortunately, not without pitfalls.

In version 1.0, a simple AJAX request used to work like this:

But with the latest version (2.0) it won’t:

The Webkit engine upgrade came with an update of the security guidelines. But it’s not too bad, there are ways around the new restrictions!

Solution #1:
-  Replace all AJAX requests with JSON-P.

This variant requires support by the requested API. As not every application offers built-in support for JSON-P, you might have to proxy all requests through some kind of wrapper-service.

However JSON-P comes with some drawbacks. it only supports GET, not POST data.

Solution #2:
- We could load data through iFrames … Seriously? Nope, thanks!

Solution #3:
- We’ll use normal AJAX requests combined with CORS.

Um, sorry? CORS = Cross Origin Ressource Sharing

With CORS you’re able to use our default AJAX request without any restrictions. There are no code changes necessary!
It can be achieved with a small fix to the API – a “new” HTTP response header:

All restrictions gone for good. Great, innit? Neither the chosen method (GET or POST) nor the requested content (JSON, XML or whatever) are gonna cause any problems.

The header is supported by almost all major browsers and definitely the best and easiest solution.

For Apache, a simple .htaccess should be enough for most scenarios:

Or in PHP:

More about CORS and other response headers: http://www.w3.org/TR/cors/#access-control-allow-origin-response-header

How to use these headers with many other platforms: http://enable-cors.org/

Rene Hennig ist Mobile Developer bei gjuce und zuständig für die Entwicklung von Mobilen Websites und Apps, Intel AppUp und diverser Mobile Frameworks.

Hinterlasse eine Antwort

Ähnliche Beiträge

  • No Related Post