• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

Use a fallback AJAX failure mechanism ...

I’ve found using one default fallback AJAX failure mechanism to be a bit of a life saver, it would prove even more useful if you are making a lot of AJAX calls distributed across your site.

I found this approach especially helpful in controlling when the AJAX failure callback is triggered, so you don’t end up in messy situaitons where your controller processes the request and the wrong view is sent back to your AJAX success callback.
An example of this being: I had some custom authentication code in the controller, if a user was not logged in the code returned the login form and an error basically. If this authentication code was processed during an AJAX request the result was passing the login form back to the AJAX success function which was confusing for the user.

Its very straight forward to take care of this using jQuery and cakePHP:

Controller takes care of the error, if the request is via AJAX return a 403 header, else display the login form with a message:

if ($this->Session->read(User.is_registrant)) {
 
//If request via ajax, return 403 header
if ($this->isAjax) {
    header("HTTP/1.0 403 Internal Server Error", true, 403);
    exit;
}
else {
    $this->Session->setFlash(__(Registrants do not have access to admin area., true));
    $this->redirect(array(controller=>users, action=>admin_login), null, true);
}
 
}

Then in jQuery you can define a single callback for AJAX failure which will check the header, if it is 403 then there has been an error with authentication and simply display a message in a floating div then reload the page, which will automatically take the user back to the login.

//Handling any ajax errors
$("#loading").ajaxError(function(event, XMLHttpRequest, ajaxOptions, thrownError){
 
 if (XMLHttpRequest.status == 403) {
 
   //Unbind ajaxStop
   $(this).unbind(ajaxStop);
 
   //Use the loading message dialog to display an error
   $(#loading-message, this).empty();
   $(#loading-message, this).append(<h2>Error!</h2>);
   $(#loading-message, this).append(<p style="text-align:left;">There has been an error with your request, it is likely your session has timed out.</p>);
   $(#loading-message, this).append(<p style="text-align:left;">This page will now be reloaded, you may be redirected to the login page if your session has expired.</p>);
 
   $("#shim").show();
   $(this).center().show(function() {
       setTimeout(function() {
           window.location.reload();
       },6000);
   });
 }
});

 Original Source:
http://deadlytechnology.com/scripts/javascript/ajax-failure-callback/

AddThis Social Bookmark Button

Posted at 01:41:19 pm | Permalink | Posted in Javascript  XmlHttpRequest  

Related Stuff

  • MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.

  • Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone

  • TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support

  • ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.

  • Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.

  • Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software

  • PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.

Top Stuff

e-messenger

MSN Web Messenger

eBuddy

ASP.NET Ajax CalendarExtender and Validation

AIM Express

Ajax Tools for ASP.NET Developers



About Ajaxlines

Ajaxlines is a project focused on providing its audience with a database of most of Ajax related articles, resources, tutorials and services from around the world.

Its purpose is to showcase the power of Ajax and to act as a portal to the Ajax development community.


Search


Topics

  • .Net (176)
  • Ajax (112)
  • Ajax Games (10)
  • Articles (95)
  • Bookmarking (35)
  • Calendar (21)
  • Chat (45)
  • ColdFusion (3)
  • CSS (84)
  • Email (23)
  • Facebook (84)
  • Flash (20)
  • Google (54)
  • Html (29)
  • Image (12)
  • International Calls & VOIP (7)
  • Java (58)
  • Javascript (280)
  • jQuery (200)
  • JSON (75)
  • Perl (2)
  • PHP (172)
  • Presentation (19)
  • Python (3)
  • Resources (2)
  • RSS (8)
  • Ruby (32)
  • Storage (4)
  • Toolkits (103)
  • Tutorials (227)
  • UI (11)
  • Utilities (174)
  • Web2.0 (18)
  • XmlHttpRequest (29)
  • YUI (13)

© 2006 www.ajaxlines.com. All Rights Reserved. Powered by IRange