razorsese Posted January 11, 2013 Share Posted January 11, 2013 How I do ajax request in MVC pattern? For example if I send ajax request to index--controller how I can returned it back to ajax because all my data is echoed in index--view. Quote Link to comment Share on other sites More sharing options...
jcbones Posted January 11, 2013 Share Posted January 11, 2013 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. Quote Link to comment Share on other sites More sharing options...
haku Posted January 12, 2013 Share Posted January 12, 2013 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. Quote Link to comment Share on other sites More sharing options...
The Letter E Posted January 14, 2013 Share Posted January 14, 2013 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.