orion- Posted November 8, 2006 Share Posted November 8, 2006 When this code is ran the XMLHttpRequestObject.status returns 0. Preventing the code inside the if to be ran. What could be the cause of this? thanks in advance [code]var offsetX, offsetY; function MouseEvent(e) { if(e) { this.e = e; } else { this.e = window.event; } if(e.pageX) { this.x = e.pageX; } else { this.x = e.clientX; } if(e.pageY) { this.y = e.pageY; } else { this.y = e.clientY; } if(e.target) { this.target = e.target; } else { this.target = e.srcElement; } } function addListener(type, callback) { if (document.addEventListener) { document.addEventListener(type, callback, false); } else if (document.attachEvent) { document.attachEvent("on"+type, callback, false); } } function removeListener (type, callback) { if (document.removeEventListener) { document.removeEventListener(type, callback, false); } else if (document.detachEvent) { document.detachEvent("on" + type, callback, false); } } function handleDown(e) { var e = new MouseEvent(e); addListener("mousemove", handleMove); addListener("mouseup", handleUp); offsetX = e.x - parseInt(e.target.style.left); offsetY = e.y - parseInt(e.target.style.top); document.getElementById("targetDiv").innerHTML = ""; } function handleMove(e) { var e = new MouseEvent(e); var x = e.x - offsetX; e.target.style.left = x + "px"; var y = e.y - offsetY; e.target.style.top = y + "px"; } function handleUp(e) { var e = new MouseEvent(e); removeListener("mousemove", handleMove); removeListener("mouseup", handleUp); var target = document.getElementById("target"); var x = parseInt(target.style.left); var y = parseInt(target.style.top); var width = parseInt(target.style.width); var height = parseInt(target.style.height); if(e.x > x && e.x < x + width && e.y > y && e.y < y + height){ var XMLHttpRequestObject = false; if (window.XMLHttpRequest) { XMLHttpRequestObject = new XMLHttpRequest(); } else if (window.ActiveXObject) { XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP"); } if(XMLHttpRequestObject) { XMLHttpRequestObject.open("GET", "text.txt"); XMLHttpRequestObject.onreadystatechange = function() { [b]if (XMLHttpRequestObject.status == 200 && XMLHttpRequestObject.readyState == 4)[/b] ) { document.getElementById("targetDiv").innerHTML = XMLHttpRequestObject.responseText; delete XMLHttpRequestObject; XMLHttpRequestObject = null; } } XMLHttpRequestObject.send(null); } } }[/code] Quote Link to comment https://forums.phpfreaks.com/topic/26612-xmlhttprequestobjectstatus-returns-0/ Share on other sites More sharing options...
ober Posted November 8, 2006 Share Posted November 8, 2006 Maybe I'm misunderstanding the syntax, because it looks like you're trying to do some JS OOP, but you can't get the status or the onreadychangestate before you do the send. Quote Link to comment https://forums.phpfreaks.com/topic/26612-xmlhttprequestobjectstatus-returns-0/#findComment-121736 Share on other sites More sharing options...
orion- Posted November 8, 2006 Author Share Posted November 8, 2006 This code came from a book "Ajax for Dummies" and I am just trying to understand why I does'nt work. Where would you suggest I would do the send? I can't place it in XMLHttpRequestObject.onreadystatechange = function() because then it would do a send every time the readystate changes, right? Quote Link to comment https://forums.phpfreaks.com/topic/26612-xmlhttprequestobjectstatus-returns-0/#findComment-121742 Share on other sites More sharing options...
ober Posted November 8, 2006 Share Posted November 8, 2006 that should be a seperate function altogether... do a search on this board for posts by me for some examples. Or look on www.ajaxfreaks.com for a tutorial on how it should be handled. Quote Link to comment https://forums.phpfreaks.com/topic/26612-xmlhttprequestobjectstatus-returns-0/#findComment-121753 Share on other sites More sharing options...
orion- Posted November 11, 2006 Author Share Posted November 11, 2006 I uncovered the source of evil, I was just opening the .html file, and not accessing it through a web server. Wen I installed Apache and accessed the file through Apache it worked perfectly.Thanks for putting your time into this. Quote Link to comment https://forums.phpfreaks.com/topic/26612-xmlhttprequestobjectstatus-returns-0/#findComment-123264 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.