Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX and the MVC pattern

Please redirect me if a similar question exists.. I haven't been able to find anything, though I'm sure that my problem is fairly common...

I have a page that has 5-6 divs that can be individually loaded through Ajax requests. Through a prototype ajax.request(), the server (php) echoes back the HTML code for the division before the client refreshes the divs's innerHTMLs.

Here's my question : What's the best practice for preserving the MVC pattern on the server side concerning the HTML code it throws out?

For now, my models return database data to the controller, enabling it to initiate a really long var containing the HTML code that it then echoes. My problem is that I end up with a lot of HTML code in my controller classes...

like image 804
koni Avatar asked Jan 24 '23 18:01

koni


1 Answers

You could use JSON for transporting data to the client-side and constructing it there. That way you'll have an abstracted data source not bound by your markup. JSON can be directly evaluated into a javascript object.

like image 67
Andy Avatar answered Feb 05 '23 23:02

Andy