AJAX requests in web site template Jun18 '06

JavaScript let’s you send data with the xmlhttprequest object, using a full-length URL, such as this:

mypage.php?today=Sunday&sky=blue&weather=80

You’ll recognize this URL from any development work you do - it simply contains variables that are passed from one page, to the next.

With JavaScript, you can request this URL, which then connects to the server, and runs through any processes on that page (mypage.php).

However, if your web site is set up as a template (possibly with mod_rewrite, which could redirect every request to one page), you may have an issue with this.

A template means that you have one page that contains the static HTML, such as <head> tags, <body> tags, and possibly the DOCTYPE, and other stuff that doesn’t change - or, at least, is consistent amongst all pages in your web site.

Every page requested on your site uses that template page, and simply fills in the content, based on the request.

This is a very good way to setup a web site, but you may run into problems when using AJAX.

Any AJAX requests (within your site template) will have the HTML tags surrounding the XML.

So, you’ll have something like this:

<html>

    <head>

    </head>

    <body>
        
        <responseXML>
            <result>Query result</result>
        </responseXML>

    </body>

</html>

An XML file (a valid XML file) can’t (and shouldn’t) have all the HTML included in it. This makes parsing it much more cumbersome, and it’s just not the right way to do it.

To get around this, you’ll need to request a file that’s not part of your template structure.

I’m open to ideas on this.

The only quick thing I can think of is requesting a file that’s off the server - possibly another domain, which could "house" AJAX request pages. This is probably not the best approach, since if that site goes down for some reason, all other sites are affected.

Categories: JavaScript , Web Development , XML

Add Feedback (view all)

Leave feedback

Feedback

Input format: The editor controls below will assist with Markdown syntax.

Status

Sub-status

Your info

Well it depends on how your template system works. Can’t you tell it to only work on certain files or folders or filename patterns? Or to e ... Read more.

Yes. In fact, every request that does actually exist as a path (ie: mydomain.com/documents/expenses.doc) should b ... Read more.

matthom is published and produced by Matt Thommes - an independent publishing enthusiast, mobile blogger, content creator, informative writer, web developer from Chicago. Never one to conform, Matt intends to promote the effect the web has on our lives, in an effort to intensify, instruct, and clarify all that is happening around us.

Contact Matt

Similar Entries

Stats

80 unique visits since August 2008

Syndicate

Advertisements