Alan M.
Alan M.

Reputation: 1369

Should I use an XMLHttpRequest /JSON or an iFrame?

I have been making extensive use of XMLHttpRequests and JSON to fetch from a MySQL database and return records as arrays. It works perfectly.

Additionally, I have three cases in which I have the server (via PHP) formatting the data as a web page and creating bar charts (as opposed to sending arrays back to JavaScript for processing). Currently, I call the PHP file via a hidden iframe and then insert the HTML into a DIV (since I don't like certain aspects of iframes and prefer to not have them accessible).

Is using an iframe the best method or can I accomplish the same thing via a XMLHttpRequest and JSON? I'm asking because of the length of the data being returned. At present, it translates to a full page of data, but I want it to be extensible (e.g., perhaps a couple of pages of data in the future).

Thank you for any insights.

Upvotes: 2

Views: 1594

Answers (1)

Branislav Abadjimarinov
Branislav Abadjimarinov

Reputation: 5131

If you want to get formatted html from other page and insert it in the current page dom the easiest way according to me is using the jQuery load methods. It make ajax request to a given url, gets the html and appends it to specified page element.

Generally the two methods - passing JSON data and passing Formatted html are good for different things. I personally prefer to pass JSON data and format it with client script and html. In this way it is more reusable.

Upvotes: 2

Related Questions