• Coding
  • AJAX vs. Standard Requests

Hey everyone,

I've been recently thinking on the best way of how to use AJAX calls to the server, but have standard requests in case AJAX requests failed for some reason (maybe JavaScript is turned off by the user or something). Or maybe in other cases, certain requests have to be standard ones, while others have to be AJAX ones.

Anyway, I was wondering how would you organize your code to keep it from turning into a huge mess. Would you handle both types of requests inside the same method? Or would you separate them into different methods?

I hope that was clear enough lol. Share your thoughts!
personally i prefer HIJAX...
so you do your code as if there weren't any ajax functionality, then you hijack this default behavior and implement ajax preferably calling another method, i don't like to rely on header sniffing(x-http-forwarded-with),
it seems more solid this way and more clear. So you achieve rock solid ajax functionality with html fallback, seo friendliness...

but anyway, as always, nothing is always true, and sometimes it depends on external factors, like timeline/budget so you can't afford the luxury of devloping the site/application in two iterations
JQuery has an extremely classy way of dealing with that. By adding event.preventDefault(); to the end of your js function you will prevent the "default" event.

Example:

HTML
<a href=www.mysite.com?http-request=post id=http-request>My link</a>
JS
$('#http-request').click(function(event){
//Code your AJAX request here
event.preventDefault();
});
If the client has Javascript enabled, the js function will be executed. At the end of the AJAX function, it will "prevent the default" action, what you called the "Standard request".

Only problem with that technique (not really a problem): you have to use JQuery.

God, I miss web development ...
Thanks for the input guys, smart way of dealing with the issue. So you basically assume that you're not adding any AJAX at first, and then you use JavaScript (jQuery is my library of choice) and intercept the request. Finally use the event.preventDefault() method to stop the standard request and inform the browser that the request has been taken care of. I like it...

I assume that would work very nicely when using PHP. But I need some advice from someone who knows ASP.NET MVC... As you might know, the ActionResult of a standard request could be different from that of an AJAX request. I was thinking of maybe doing the following:
public ActionResult DoSomething() {
   if(Request.IsAjaxRequest()) {
      //prepare the result
       return Json(result);
   } else {
      //prepare the result
      return View(viewModel);
   }
}
Or maybe I could separate that method into two. One called DoSomething() for standard requests, and DoAJAXSomething() for AJAX requests. But in this case, how do I handle that in my View file?
rahmu wroteGod, I miss web development ...
You gotta love web development. It's literally addictive ;)

@Admin: could we get some code editor with code and tabbing when trying to post some code?