0

I'm a semi newbie so please bear with me... Note, I don't know either jQuery or Json at this point

In my MVC project (I'm not using a framework but the project combines a front controller with an MVC), I have:
1) a Controller, which sends some parameters to a DAO. The DAO runs a MySQL query and sends back to the Controller an array of articles.
2) a View layer where I want the user to be able to click a button to move from article to article. The way I'm proposing to do that is by a javascript Ajax call to get the next article in the array generated in the Controller.

My question is: what should be the URL called by the Ajax function? Obviously it cannot call the Controller (or can it?). Should I add a class of dedicated Ajax content vessels that the Controller would instantiate with the array? I have difficulties seeing how the View would find the right URL... Should the Controller pass the parameters to the View and let it request the query?

JDelage
  • 13,036
  • 23
  • 78
  • 112

3 Answers3

2

The XHR (also known as AJAX) calls are no different at controller level, then classical browser requests. The difference is only in what you expect to receive in response.

This means that, if you have fully realized views (no just dumb templates), the type of request should be important only to the views. You can easily distinguish them by adding extensions:

  • http://foo.in/user/list - simple request
  • http://foo.in/user/list.json - XHR request

The difference gets recognized mostly at the routing mechanism, which them sets specific details on the Request instance. When controller sees that Request instance has a isXHR flag, it tells the view: "Respond to this with something, that is not full HTML page".

Basically, the same controllers should handle both the normal and XHR calls. In fact, you do not care about, what type of request it is. Only whether you need to produce html, xml or json in the response.

P.S.: model layer should be completely unaffected by the type of requests.

tereško
  • 58,060
  • 25
  • 98
  • 150
  • is this only way? There's another - more common way to distinguish them via `$_SERVER['HTTP_X_REQUESTED_WITH']` – Yang Dec 01 '12 at 20:11
  • 1
    `HTTP_X_REQUESTED_WITH` is sent by JS frameworks. But it should b possible to sent some signature header using native `XMLHttpRequest` object. But it does not change the point. The controllers behavior should not be affected by fact that request was made via XHR. – tereško Dec 01 '12 at 20:13
  • @tereško - I appreciate the effort you put in answering my question but I have to admit that I am completely lost. I'm afraid you assume a higher level of knowledge on my part than is the case. :) – JDelage Dec 01 '12 at 21:03
0

From the list of above posts I assume you must know the Ajax syntax to call a method while editing articles.

How to do so is as follows: 1. Initially define an action inside your controller which servers your purpose (May be editing your articles in this context.) 2. Through ajax method specify the Controller and the action which you wants to call. ( At this juncture it should be Articles -- Controller, EditArticle -- Action).

The control automatically navigates to the particular action method.

Regards Pavan.G

Pavan Kumar
  • 190
  • 2
  • 8
-3

Depends on the framework you use. But generally:

You can use the Controller with sending a "flag" (in a GET variable for example), that it is a AJAX query, and then exit the function, but having different Controllers for AJAX queries are considered a nicer route :) Anyways, something similar to this:

function page() {
   if($_GET["is_ajax"] == "1") {
      // return the AJAX query
      return;
   }
// go on with showing the page
}

Hope this helps!

dande
  • 233
  • 2
  • 11