Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 4.00 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-20-2006, 11:26 AM   PM User | #1
spenoir
New Coder

 
Join Date: Feb 2004
Location: london
Posts: 77
Thanks: 0
Thanked 0 Times in 0 Posts
spenoir is an unknown quantity at this point
AJAX not working in IE

Anyone Know why my first ajax script doesn't work in IE. It works in Firefox and Opera. I've attached the xml, js and html in a zip.

Thanks in advance.


Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script language="javascript">
var req=null;
var console=null;
var READY_STATE_UNINITIALIZED=0;
var READY_STATE_LOADING=1;
var READY_STATE_LOADED=2;
var READY_STATE_INTERACTIVE=3;
var READY_STATE_COMPLETE=4;

function initXMLHTTPRequest () {
    var o;
    try{
        o = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
        try{
            o = new ActiveXObject("Microsoft.XMLHTTP");    
        }catch(e){
            o = false
        }
    }    
    if(!o && typeof XMLHttpRequest!='undefined'){
        o = new XMLHttpRequest();
    }
//    o.async=false;
    return o;
}
function sendRequest (url,params,HttpMethod) {
	if (!HttpMethod) {
	HttpMethod="GET";	
	}
	req=initXMLHTTPRequest();
	if (req) {
	req.onreadystatechange=onReadyState;
	req.open(HttpMethod,url,true);
	req.setRequestHeader ("Content-Type", "text/xml");
	req.send(params);
	}
}
function onReadyState() {
	var ready=req.readyState;
	var data=null;
	if (ready==READY_STATE_COMPLETE) {
		data=req.responseText;
		setDataXML(req);
	}
	else {
		data="loading...["+ready+"]";
	}

}

window.onload=function() {
	document.getElementById('writeroot');
	sendRequest("books.xml");
}

function setDataXML(req)
{
	var books = req.responseXML.getElementsByTagName('book');
	for (var i=0;i<books.length;i++)
	{
		var x = document.createElement('div');
		x.className = 'book';
		var y = document.createElement('h3');
		y.appendChild(document.createTextNode(getNodeValue(books[i],'title')));
		x.appendChild(y);
		var z = document.createElement('p');
		z.className = 'moreInfo';
		z.appendChild(document.createTextNode('By ' + getNodeValue(books[i],'author') + ', ' + getNodeValue(books[i],'publisher')));
		x.appendChild(z);
		var a = document.createElement('img');
		a.src = books[i].getElementsByTagName('cover')[0].getAttribute('src');
		x.appendChild(a);
		var b = document.createElement('p');
		b.appendChild(document.createTextNode(getNodeValue(books[i],'blurb')));
		x.appendChild(b);
		document.getElementById('writeroot').appendChild(x);
	}
}

function getNodeValue(obj,tag)
{
	return obj.getElementsByTagName(tag)[0].firstChild.nodeValue;
}
</script>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>

<div id="writeroot"></div>

</body>
</html>
Attached Files
File Type: zip ajaxbooks.zip (2.4 KB, 530 views)
__________________
99 times out of 10!
spenoir is offline   Reply With Quote
Old 04-20-2006, 01:32 PM   PM User | #2
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
I glanced at it real quick.

You should be doing the check for ActiveX after you do the check for the native XMLHttpRequest() object. Plus it is better o use object detection.

You also need to set the onreadystatechange after the open

req.open(HttpMethod,url,true);
req.onreadystatechange=onReadyState;

Now when it does not work when does it fail? Put debug statemens in the code to see where IE is not performing correctly. Does it die in the request, the XML function. ETC.

Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

Last edited by A1ien51; 04-20-2006 at 01:43 PM..
A1ien51 is offline   Reply With Quote
Old 04-20-2006, 01:45 PM   PM User | #3
spenoir
New Coder

 
Join Date: Feb 2004
Location: london
Posts: 77
Thanks: 0
Thanked 0 Times in 0 Posts
spenoir is an unknown quantity at this point
The script doesn't give any error, it just doesn't display anything which makes it hard to debug I've made those amends you have suggested but it doesn't seem to make any difference.
There can't be a lot wrong with it as IE doesn't give an error, just a very unhelpful blank screen.
__________________
99 times out of 10!

Last edited by spenoir; 04-20-2006 at 02:00 PM..
spenoir is offline   Reply With Quote
Old 04-20-2006, 03:11 PM   PM User | #4
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
It is not hard to debug. Add alert statements in there and see where it stops working. I can not look at the code in detail until tonight, hopefully someone else around here can.

Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 05-16-2006, 11:45 AM   PM User | #5
spenoir
New Coder

 
Join Date: Feb 2004
Location: london
Posts: 77
Thanks: 0
Thanked 0 Times in 0 Posts
spenoir is an unknown quantity at this point
I've added alert statements to this script from top to bottom and IE seems to read the entire script. Has anyone got any other ideas for getting this ajax to work in IE. I understand it could be the structure which I'm going to look at changing, as mentioned above by Eric.

many thanks
__________________
99 times out of 10!
spenoir is offline   Reply With Quote
Old 05-16-2006, 12:28 PM   PM User | #6
NancyJ
Senior Coder

 
NancyJ's Avatar
 
Join Date: Feb 2005
Location: Bradford, UK
Posts: 3,162
Thanks: 19
Thanked 65 Times in 64 Posts
NancyJ will become famous soon enough
books has 0 length in ie.
__________________
http://www.hazelryan.co.uk
NancyJ is offline   Reply With Quote
Old 05-16-2006, 05:36 PM   PM User | #7
KC-Luck
Regular Coder

 
Join Date: Aug 2005
Posts: 282
Thanks: 0
Thanked 0 Times in 0 Posts
KC-Luck is an unknown quantity at this point
This is another case of, if you did this code on a server, it would work fine.
but if you run this via file: protocol, you have to use another line of code to enable the use of reponseXML.

Place the following code just before your setDataXML(req); and all should work fine for file: protocol.
Code:
if (!req.responseXML.documentElement && req.responseStream)
  req.responseXML.load(req.responseStream);
KC-Luck is offline   Reply With Quote
Old 05-24-2006, 04:42 PM   PM User | #8
spenoir
New Coder

 
Join Date: Feb 2004
Location: london
Posts: 77
Thanks: 0
Thanked 0 Times in 0 Posts
spenoir is an unknown quantity at this point
Nice one KC-Luck, its working fine now in IE.
Thanks a million.
__________________
99 times out of 10!
spenoir is offline   Reply With Quote
Old 06-23-2006, 02:10 PM   PM User | #9
spenoir
New Coder

 
Join Date: Feb 2004
Location: london
Posts: 77
Thanks: 0
Thanked 0 Times in 0 Posts
spenoir is an unknown quantity at this point
selecting 2 nodes at random

Hi all,
I've added the var r to my script below. I'm trying to use it select 2 xml nodes at random:

Code:
function setDataXML(req)
{
	var r = Math.floor(Math.random() * 10) + 2;
	var books = req.responseXML.getElementsByTagName('book');
	
	if (!req.responseXML.documentElement && req.responseStream) {
  		req.responseXML.load(req.responseStream);
	}
	for (var i=0;i< books.length;i++)
	{
		var x = document.createElement('div');
		x.className = 'book';
		var y = document.createElement('h3');
		y.appendChild(document.createTextNode(getNodeValue(books[i],'title')));
		x.appendChild(y);
		var z = document.createElement('p');
		z.className = 'moreInfo';
		z.appendChild(document.createTextNode('By ' + getNodeValue(books[i],'author') + ', ' + getNodeValue(books[i],'publisher')));
		x.appendChild(z);
		var a = document.createElement('img');
		a.src = books[i].getElementsByTagName('cover')[0].getAttribute('src');
		x.appendChild(a);
		var b = document.createElement('p');
		b.appendChild(document.createTextNode(getNodeValue(books[i],'blurb')));
		x.appendChild(b);
		document.getElementById('writeroot').appendChild(x);
	}
}
I think that the for statement needs modifying something like following:

Code:
	for (var i=0;i< books.r;i++)
This doesn't work however.

I'm sure i'll get there in the end but any help would be great.

cheers
__________________
99 times out of 10!
spenoir is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:29 PM.


Advertisement
Log in to turn off these ads.