tags:

views:

60

answers:

4

Hi folks, I'm still pretty new to AJAX and javascript, but I'm getting there slowly.

I have a web-based application that relies heavily on mySQL and there are individual user accounts that are accessed and the UI is populated with user specific data.

I'm working on getting rid of a tabbed navigation bar that currently loads new pages because all that changes from page to page is information within one box.

The thing is that box needs to reload info from the database, etc.

I have had great help from users here showing that I need to call the database within the php page that ajax is calling.

OK-so pardon the lengthy intro-what I'm wondering is are there any specific limitations to what ajax can call that I need to know about? IE: someone mentioned that it's best not to call script files and that I should remove scripts from the php page that is being called and keep those in the 'parent' page. Any other things like this I need to keep in mind?

To clarify: I'm not looking to discuss the merits/drawbacks of the technology. I'm wondering about specific coding implementation that I need to be aware of (for example-I didn't until yesterday realize that if even if I had established a mySQL connection on the page, that I would need to re establish that connection in my called page as well...makes perfect sense now).

A: 

IMHO, it's best to put your client side javascript in a separate page and then import it - neater container. one thing I've faced before is how to call xml back which contains code to run such as more javascript - it's worth checking if this is likely earlier on and avoiding, than having to look at evals.

Mildly interesting.

Mark Lewis
A: 

First, you can't have Javascript embedded in the HTTP response to an AJAX call. That's a security issue.

No mention of the dynamics of the database, but if the data to be displayed in tabs doesn't have to be real-time, why not cache it server-side?

stillstanding
Yeah I was referring to the fact that as AJAX responses are just text, it's possible to do different stuff with it such as the use of `eval()`. I've edited my post including a reference.
Mark Lewis
eval on an HTTP response is way too dangerous and makes your app vulnerable to an XSS attack! never even think of using it.
stillstanding
+3  A: 

XMLHttpRequest which powers ajax has a number of limitations. I recommend brushing up on the same origin policy. This is a pivotal rule because it limits where AJAX calls can be made.

Rook
for good reason... however you can get around the limitations of XMLHttpRequest using on-demand javascript: http://ajaxpatterns.org/On-Demand_Javascript
Polaris878
A: 

I find that like any other protocol, Ajax works best in tightly controlled conditions. It wouldn't make much sense for updating nearly the whole page, unless you find that the user experience is improved with an on-page 'loader'. Without going into workarounds, disadvantages will include losing the browser back button / history, issues such as the one your friend mentioned, and also embedded resources and other rich content can suffer as well, and just having an extra layer of complexity to deal with in your app. Don't treat it as magic sauce for your app - make sure every use delivers specific results that benefit your client / audience.

Oleg