Skip to content
/ XHR Public
forked from Topener/XHR

Super awesome HTTP Client for Appcelerator Titanium :)

License

Notifications You must be signed in to change notification settings

jkotchoff/XHR

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

What is it?:

XHR is a wrapper around Titanium's HTTPClient. It works perfectly with REST API endpoints and has a built in cache system that you can use for your requests.

Usage:

In your app.js (or elsewhere), call:

var XHR = require("/lib/xhr");
var xhr = new XHR();
xhr.get("http://freegeoip.net/json/", onSuccessCallback, onErrorCallback, options);

Last-Modified header caching

This project fork has provided an API for titanium projects to be able to perform HTTP requests and cache the responses locally until the Last-Modified header changes on the server.

With Ruby on Rails, the Last-Modified header for GET requests can be set using stale?: ActionController::ConditionalGet.stale?

What this means for the Titanium app is that when we use the following code:

var XHR = require("/lib/xhr");
var xhr = new XHR();
xhr.getIfModified("http://freegeoip.net/json/", onSuccessCallback, onErrorCallback, options);

It first sends a HTTP HEAD request to:

http://freegeoip.net/json/

and it then sends a HTTP GET request to retrieve the result and cache it locally to disk.

Next time the above code snippet is called, if the Last-Modified header from the response hasn't changed, it will load the content directly from disk.

For big JSON requests that don't change often, this saves a lot of HTTP traffic between the client and the server.

Ideally, this technique wouldn't require both a HEAD request and a GET request.

Unfortunately, the Titanium HTTP client (as of Titanium version 3.0.2), does not correctly set response headers as per: http://developer.appcelerator.com/question/150199/getresponseheader-not-working-in-onreadystatechange-function

and therefore at this time, both a HEAD and a GET request are neccessary.

About:

Created by Raul Riera, @raulriera
Contributions by Daniel Tamas, @dan_tamas and Bob Sims, @2wheelsburning

About

Super awesome HTTP Client for Appcelerator Titanium :)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%