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 a suburb of 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

Popular Pages

  1. Fast rounded corners in Photoshop (7873 recent visits)
  2. PHP – passing variables across pages (2857 recent visits)
  3. JavaScript set selected on load (2345 recent visits)
  4. Removing all child nodes from an element (1704 recent visits)
  5. iPod songs out of order? (1412 recent visits)
  6. Firefox 3 smart address bar: wildcard search (1300 recent visits)
  7. Britney - Everytime piano tab (1180 recent visits)
  8. MySQL LEFT JOIN syntax (971 recent visits)
  9. Breathe Me - Sia (824 recent visits)
  10. Tumblr: how blogging should be (727 recent visits)

Similar Entries

Stats

24 unique visits since August 2008

Syndicate

Advertisements