Lightweight XmlHttpRequest wrapper

I created a simple wrapper for the XMLHttpRequest object. The code below should be enough to get you started. You probably want to add in some better error handling.

function ajax(cfg)
{
    var xhr,
        url = cfg.url,
        method = cfg.method || 'GET',
        success = cfg.success || function () {},
        failure = cfg.failure || function () {};
 
    try {
        xhr = new XMLHttpRequest();
    } catch (e) {
        xhr = new ActiveXObject("Msxml2.XMLHTTP");
    }
 
    xhr.onreadystatechange = function ()
    {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                success.call(null, xhr);
            } else {
                failure.call(null, xhr);
            }
        }
    }
 
    xhr.open(method, url); 
    xhr.send(null);
}

Usage:

ajax({url:'/path/to/servlet',success: someFunction, failure: someFunction});

Making a HTTP POST request with NSURLConnection

To make an asynchronous HTTP POST request from your iPhone application with NSURLConnection:

responseData = [[NSMutableData data] retain];
 
NSMutableURLRequest *request = [NSMutableURLRequest 
									requestWithURL:[NSURL URLWithString:@"http://www.domain.com/your/servlet"]];
 
NSString *params = [[NSString alloc] initWithFormat:@"foo=bar&key=value"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];

Use the - (void)connectionDidFinishLoading:(NSURLConnection *)connection method in your class to handle the response.