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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.