...

View Full Version : XMLHttp Mozilla Awkwardness



DavidC
09-19-2005, 06:49 PM
Hi,

I am using XMLHttp to load data from a database without a
page.
It is working perfect in Internet Explorer, but it is giving me
troubles in Mozilla Firefox.

This is the code in which XMLHttp is initialized and the function
which is used to load a page:



var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}

function loadFragmentInToElement(fragment_url, element_id) {
var element = document.getElementById(element_id);

xmlhttp.open("GET", fragment_url);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

element.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null);
}

I now call loadFragmentInToElement in an onClick event handler of
a link, as follows:



function showContact() {
loadFragmentInToElement(baseurl + 'get_contact.php', 'content');
}


In this baseurl is a variabele which is set to the path of get_contact.php. Furthermore on the page a div exists with id = 'content', in which the result of get_contact.php should be loaded.
Now it is the case that in IE indeed get_contact.php is loaded into this
div, but in Mozilla nothing happens.

Now the strangeness begins:
When I add some 'alert' in showContact, just before calling loadFragmentInToElement, the alert shows up and afterwards get_contact.php is loaded into the div, also in Firefox.

So by adding something like: alert('hi'); in front of the call to the function
it suddenly works in Mozilla.

Does anybody know what causes this and, even better, a solution for this problem?

Regards David

glenngv
09-20-2005, 04:06 AM
Try specifying true to make an asynchronous request.

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

DavidC
09-20-2005, 12:42 PM
This did not make a difference unfortionately.
No extra information could be retrieved after adding the true which could be relevant to this topic.

glenngv
09-21-2005, 04:48 AM
What extra information?

DavidC
09-21-2005, 11:30 AM
That remark was added to stress the fact that it made not difference and thus there is no extra information (like error output or something like that) that I can add to my post after trying your suggestion.

KC-Luck
09-21-2005, 04:14 PM
works for me! :rolleyes:

<html>
<head>
<script>
var xmlhttp;
try{xmlhttp = new XMLHttpRequest();}catch(e){
try{xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");}catch(e){
try{xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}catch(e){}}}
function go() {
if(!xmlhttp)return alert("No Http Transport.");
xmlhttp.open("GET",document.location.href,true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
document.getElementById("output").innerHTML =
"<textarea rows=\"20\" cols=\"60\">" +
xmlhttp.responseText + "</textarea>";
}
}
xmlhttp.send(null);
}
</script>
</head>
<body>
<button onclick="go()">Go</button>
<div id="output"></div>
</body>
</html>

DavidC
09-22-2005, 03:13 PM
It still does not work for me, at least not al the time.

This is the output of the javascript console



Error: uncaught exception: [Exception... "Component returned failure code: 0xc1f30001 (NS_ERROR_NOT_INITIALIZED) [nsIXMLHttpRequest.send]" nsresult: "0xc1f30001 (NS_ERROR_NOT_INITIALIZED)" location: "JS frame :: http://localhost/heco/js/xmlhtml.js :: loadFragmentInToElement :: line 17" data: no]

glenngv
09-23-2005, 07:06 AM
Try initializing xmlhttp everytime loadFragmentInToElement() is called.

var xmlhttp;

function loadFragmentInToElement(fragment_url, element_id) {
var element = document.getElementById(element_id);

/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@end @*/

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}

xmlhttp.open("GET", fragment_url);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

element.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null);
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum