MVC ajax calls - where to handle them?

I have a scooter MVC infrastructure that I create, and up to this point I have managed to avoid the need for any AJAX calls. Now, however, I would like to create a real-time update feed.

My question is: where are the handlers for ajax calls usually stored in MVC? Should I store them in the same controller that is involved in the call?

For example, if my domain www.example.com/browse/blogs (browse is the controller, blogs is the method) makes an AJAX call for the updated list of blogs, will it just be a call to www.example.com/browse/update_list or whatever something else?

OR, is this true for a separate AJAX controller? www.example.com/ajax/update_blogs

How do you do this?

+5
source share
3 answers

I would say that the Ajax request is exactly the same as non-Ajax: it works exactly the same, in fact, from the point of view of the HTTP protocol.

The only difference is that you are returning some unformatted data, such as JSON or XML (hey, this is the same as generating an ATOM ^^ feed) or just part of the HTML page.

So, I would consider them like any other β€œnormal” HTTP request and place them the way I would like for requests without Ajax.


An alternative can be only one action in your controller: /browse/blogs- and always call it.

But he would detect if he would be through an Ajax request or not, and would be:

  • returns a full page if called through a "normal" request
  • ( ), Ajax

: "" ; , Zend Framework (. 12.8.4.3. ContextSwitch AjaxContext)

+2

, AJAX- , , AJAX. AJAX, , , .

, AJAX , , - (, , ) . , Controller, AJAX:

protected function isAjax()
{
    return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
            $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest');
}
+8

Even if you are not using asp.net MVC, I would recommend that you familiarize yourself with the nerd tutorial, in particular in the AJAX section. this will help answer some of your design questions.

They have a separate action on the same controller.

http://www.wrox.com/WileyCDA/Section/id-321793.html

0
source

All Articles