Jump to content

MVC and Ajax


razorsese

Recommended Posts

You should have a data handling script, that returns a json encoded array. It's the way I would do it. As MVC all interacts together, it would work the same as a regular request, it is just that the request isn't loading a new page. It is just getting the data, and inserting it into an existing page.

Link to comment
https://forums.phpfreaks.com/topic/273029-mvc-and-ajax/#findComment-1405012
Share on other sites

MVC means you have a controller that uses one method to generate data, and another method to convert that data into a usable format. The data may be formatted as HTML, XML, JSON, text files or any other format that is necessary. This is why the data is generated separately, so that one method can be used to generate the data regardless of the format of the output. Then multiple methods can be created to format the data.

 

For web based MVC frameworks, the data is initially outputted to the screen as HTML. Ajax requests however rarely need HTML, JSON or XML are generally preferred. So the system outputs the data using one of these formats. This data is returned to the AJAX, and the ajax can use it to alter the page.

 

So do to this, you need to register a callback path on your system somehow. This may be a separate document which your ajax script points to, or if you are using some framework/CMS, they will give you a method of creating new paths within the system. Regardless of how you do this, in the callback script, you will have your MVC system generate the data, and run it through some kind of formatter that translates it into a format you can use.

Link to comment
https://forums.phpfreaks.com/topic/273029-mvc-and-ajax/#findComment-1405121
Share on other sites

In MVC I've found it's more organized to build a set of api controllers to handle any ajax calls you may be using. If you're using a framework like code igniter or zend you can also find nice REST API libraries available that support format switching via url params so you can get nice responses in XML, JSON, HTML or PHP for eval.

Link to comment
https://forums.phpfreaks.com/topic/273029-mvc-and-ajax/#findComment-1405635
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.