Using MochiKit doXHR

The doXHR() function in MochiKit is pretty handy for doing AJAX FORMs. It is a shorthand function for do XMLHttpRequest. Here is some basic usage of doXHR(). I was using this code to save position of a Google Map marker via AJAX POST. The URL called returns a string that gets stuffed into responseText of the result object.

saveMapLocation: function( ) {
  //values that i am passing are latitude and longitude
  var qry = queryString({lat:latitude.value,lon:longitude.value});
  var d = doXHR(url, 
    {
      method:'POST',
      sendContent:qry,
      headers: {"Content-Type":"application/x-www-form-urlencoded"} 
    });
    //this callback fires when the XHR returns successful
    d.addCallback(function (result) 
    { 
      log('save successful');
    });

    //this callback fires when the XHR fails
    d.addErrback( function (result) 
    { 
      log('save failed');
    });

MochiKit Async documentation