MVC ajax calls - where to handle them?

前端 未结 3 1653
不思量自难忘°
不思量自难忘° 2021-01-03 02:57

I have a self-rolled MVC framework that I am building, and up to this point have managed to avoid the need for any AJAX calls. Now, however, I\'d like to create a real-time

3条回答
  •  星月不相逢
    2021-01-03 03:19

    I'd say an Ajax request is exactly the same as a non-Ajax one : it works exactly the same way, actually, from a point of view of HTTP Protocol.

    The only difference is that you are returning some non-formated data, as JSON or XML (hey, this is the same as generating an ATOM feed ^^ ), or only a portion of an HTML page.

    So, I would treat those as any other "normal" HTTP request, and place them the way I would for non-Ajax requests.


    A semi-alternate idea might be to have only one action in your controlller : /browse/blogs -- and always call that one.

    But, it would detect if it's being via an Ajax request or not, and would :

    • return a full page if called via a "normal" request
    • or return only some data (or a portion of the page) if called via an Ajax request

    Note : that's not a "wild" idea ; Zend Framework, for instance, provides some stuff to facilitate that (see 12.8.4.3. ContextSwitch and AjaxContext )

提交回复
热议问题