Jump to content

XMLHttpRequest not executed


yoshida

Recommended Posts

Hi guys, I'm building a basic AJAX-enabled page, nothing too complicated.Except the file dummy.txt isn't loading into the div with ID "main". The div called 'main' stays empty. Can anyone tell what's going wrong?Here's the script:

var xmlhttp = false;try {	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");} catch (e) {	try {		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");	} catch (E) {		xmlhttp = false;	}}if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {	xmlhttp = new XMLHttpRequest();}var objID = "main";var serverPage = "dummy.txt";var obj = document.getElementById(objID);xmlhttp.open("GET", "serverPage");xmlhttp.onreadystatechange = function() {	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {		obj.innerHTML = xmlhttp.responseText;	}}xmlhttp.send(null);

And here's the html file:

<DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Insert title here</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><script type="text/javascript" src="igor.js"></script><link rel="stylesheet" type="text/css" href="main.css" /></head><body><div id="cont">	<div id="head">			</div>	<div id="main">			</div></div></body></html>

Help appreciated.

Link to comment
Share on other sites

You put the variable serverPage in quotation marks in your xmlhttp.open() function. Try changing

xmlhttp.open("GET", "serverPage");

to

xmlhttp.open("GET", serverPage,true);

Link to comment
Share on other sites

You. Rock. Hardcore.However, this only works when I put the sequence into a function, and call it as an onload attribute in the body tag (or as an onclick attribute in a link). It's nice to have it work as a controlled event, but it still looks odd to me.Many thanks. I started doubting both my sanity and my skills.EDIT:I can assign a parameter to the loadMainBody() function, like loadMainBody(getpage.php?page=contact). Can I use this in an event trigger to give a value to the variable serverPage? It should then tell a serversided script what information it should fetch from the database....yeah, that's me. Each solution spawns a sh!tload of potential.

Link to comment
Share on other sites

Yes, why not?

function loadMainBody(serverPage) {	// Code}

Just don't forget to call it with the argument in quotation marks, eg.

loadMainBody('getpage.php?page=contact')

Don't worry, we all make mistakes like that sometimes.

Link to comment
Share on other sites

Yes, why not?
function loadMainBody(serverPage) {	// Code}

Don't worry, we all make mistakes like that sometimes.

OK so serverPage gets the parameter of whatever you put in the event trigger.Never imagined AJAX was the easy part of JavaCrypt. I'm on my way to update my existing database-driven template.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...