I was playing around with XMLHTTPRequests recently, let me just start this by saying I prefer to never have a single JavaScript requirement... so my use of the request is merely... icing on a big webcake.

anyway.. In offline testing I can create requests ONLY in IE... in online tests I can create requests ONLY in FF. :| my js file doesn't change, I always request a local (same folder) file... ee.

It's annoying.. I figured the FF problem would go away when I put the file online, as these things do with FF, but I didn't expect the IE one to start failing.

Does anyone have experience of using the XMLHttpRequest object in IE6, in an online environment? The only reason I can see this being restricted is that IE has the worst (non-existant) local-file-system protection imaginable, and MS decided its better to be safe than sorry (sadly, all in the wrong order). Do any IE7 users know if this works in 7? From the Beta; I hate IE7, and I know if I download it that it'll systematically destroy my computer's knowledge of IE6...

:rolleyes:

Recommended Answers

All 4 Replies

Are you using an "AJAX" lib or did you write your own XMLHTTPRequest Functions? Could you post them if you did or the lib you're using?

That is a weird scenario..

Do you have any out of the ordinary DNS setups? Like domain pointers, cause they cause problems.

A thing to note is the cross-domain restriction is the same as in cookies, so the problems are similar.

Like forgetting that a browsr will treat: http://www.example.com and http://example.com as two different domains.

Usually on a localhost it should always work since you have http://localhost/ as the domain at all times, unless you've set up subdoamins or a public domain.

Also, what error messages are you getting?

Usually you'll want to catch your errors triggered by XMLHTTPRequest, as in development there will be many, and even when implemented.

If its domain related the error will be pemission denied to call [some method] etc. etc.

well. i did test in IE7 on my work pc, and it worked :|

the code is adapted from an online example, I'd never used XMLHTTPRequests before:

function request_message(path,field,default_message){
 if(default_message){
  showMessage(default_message);
 }
 var object = request_createObject();
 var input = document.getElementById(field);
 var uri = path + "?" + input.name + "=" + input.value + "&";
 if(object){
  object.open("GET", uri,true);
  object.onreadystatechange=function() {
   if (object.readyState==4) {
    hideMessage();
    insert_message(object.responseText);
   } 
  }
 object.send(null);
 }else{
  hideMessage();
  alert("Couldn't create the request object. Maybe your security settings prevented it.");
 }
}
// XMLHTTP setup code "borrowed" (with many thanks) from [URL]http://www.jibbering.com/2002/4/httprequest.html
function request_createObject(){
 var xmlhttp=false;
 /*@cc_on @*/
 /*@if (@_jscript_version >= 5)
  try {
   xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
   try {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (E) {
    xmlhttp = false;
   }
  }
 @end @*/
 if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  try {
   xmlhttp = new XMLHttpRequest();
  } catch (e) {
   xmlhttp=false;
  }
 }
 if (!xmlhttp && window.createRequest) {
  try {
   xmlhttp = window.createRequest();
  } catch (e) {
   xmlhttp=false;
  }
 }
 return xmlhttp;
}

on firefox and ie7 i get back the correct message from whatever local/relative path I query... on ie6 i can't even create the object when my script is online :/

Have you tried a different AJAX lib to see if its specific to the one you're using?

Heres the one I use:

/**
* Fiji Web Design AJAX lib
* @copyright (c) http://www.fijiwebdesign.com
* @email fwd_ajax.class@fijiwebdesign.com
* @package Testing 
* Custom Library for 'simplifying' use of XMLHTTPRequest
*/
 
function fwd_XMLHTTP(log_id) {
	if (log_id) {
		this.setLogger(log_id);
	}
	this.ini();
	if (!this.newXMLHttpRequest) {
		this.logger( fwd_XMLHTTP.ERR_NO_XMLHTTP );
		return false;
	}
}

// properties
fwd_XMLHTTP.prototype.newXMLHttpRequest = false;
 
// static global properties
fwd_XMLHTTP._XMLHTTP_VER = false; // MSIE
fwd_XMLHTTP.ERR_NO_XMLHTTP = 'Your Browser does not support XMLHttpRequest!';
fwd_XMLHTTP.XML_HTTP_SUPORT_MOZ = 'XMLHttpRequest Support: Native';
fwd_XMLHTTP.XML_HTTP_SUPORT_IE = 'XMLHttpRequest Support: ';

/**
* Get a Reference to the xmlHTTPRequest Object
*/
fwd_XMLHTTP.prototype.ini = function() {
	if (window.XMLHttpRequest) {
		fwd_XMLHTTP.prototype.newXMLHttpRequest = function() {
			return (new XMLHttpRequest());
		};
		this.logger(fwd_XMLHTTP.XML_HTTP_SUPORT_MOZ);
    } else if (window.ActiveXObject) {
		// find latest XMLHTTP implementation on IE
		var versions = [
		"Msxml2.XMLHTTP.7.0", 
		"Msxml2.XMLHTTP.6.0", 
		"Msxml2.XMLHTTP.5.0", 
		"Msxml2.XMLHTTP.4.0", 
		"MSXML2.XMLHTTP.3.0", 
		"MSXML2.XMLHTTP",
		"Microsoft.XMLHTTP"];
		var n = versions.length;
		for (var i = 0; i <  n; i++) {
			try {
				fwd_XMLHTTP._XMLHTTP_VER = versions[i];
				if (new ActiveXObject(fwd_XMLHTTP._XMLHTTP_VER)) {
					fwd_XMLHTTP.prototype.newXMLHttpRequest = function() {
						return (new ActiveXObject(fwd_XMLHTTP._XMLHTTP_VER));
					};
					this.logger(fwd_XMLHTTP.XML_HTTP_SUPORT_IE+fwd_XMLHTTP._XMLHTTP_VER);
					break;
				}
			} catch (e) {
				// try next version
			}
		}
    }
};

/*
* Check if XMLHttpRequest is not supported
*/
fwd_XMLHTTP.prototype.check = function() {
	return this.newXMLHttpRequest ? true : false;
};

/*
* A HTTP GET
*/
fwd_XMLHTTP.prototype.get = function(url, callback, async, user, pass) {
	return this.request(url, 'GET', null, callback, async, user, pass);
};

/*
* A HTTP POST
*/
fwd_XMLHTTP.prototype.post = function(url, callback, async, user, pass) {
	return this.request(url, 'POST', null, callback, async, user, pass);
};

/*
* Private XMLHttpRequest Call
*/
fwd_XMLHTTP.prototype.request = function(url, method, data, callback, async, user, pass) {
	
	if (!this.newXMLHttpRequest) {
		this.logger( fwd_XMLHTTP.ERR_NO_XMLHTTP );
		return false;	
	}
	
	// we need to create a closure and send a new XMLHttpRequest Object to callback();
	var xmlhttp = this.newXMLHttpRequest();
	var self = this;
	
	async = (typeof(async) != 'undefined' && !async) ? false : true;
	method = method.toUpperCase();
	
	xmlhttp.onreadystatechange = function() {
		try {
			callback(xmlhttp, self);
			if (xmlhttp.readyState == 4) {
				xmlhttp, self = null; // remove closure
			}
		} catch(e) { 
			self.logger(e);
		}
	}
	
	xmlhttp.open(method, url, async, user, pass);
	if (method == 'POST') {
		xmlhttp.setRequestHeader("Method", "POST "+url+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	}
		
	this.logger('fwd_XMLHTTP '+(async ? 'Async' : 'Sync')+' '+method+' Request:'+url);
	this.logger('data:'+data ? data : '');
	
	//xmlhttp.send(data);
	return xmlhttp;
};

/**
* Execute the JSON text
*/
fwd_XMLHTTP.prototype.execJSON = function(xmlhttp) {
	if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200) {
			return eval('('+xmlhttp.responseText+')');
		}
	}
};

/**
* encode passed http vars
*/
fwd_XMLHTTP.prototype.encode = function(uri) {
    if (encodeURIComponent) {
        return encodeURIComponent(uri);
    }
    if (escape) {
        return escape(uri);
    }
};

/**
* dencode passed http vars
*/
fwd_XMLHTTP.prototype.decode = function(uri) {
    uri = uri.replace(/\+/g, ' ');

    if (decodeURIComponent) {
        return decodeURIComponent(uri);
    }
    if (unescape) {
        return unescape(uri);
    }
    return uri;
};

/**
* Set the id of the element to write logs to
*/
fwd_XMLHTTP.prototype.setLogger = function(id) {
	this.log_id = id;
}

/**
* Simple Logging utility, overload if necessary
*/
fwd_XMLHTTP.prototype.logger = function(txt) {
	if (!this.log_id) return;
	var logger = document.getElementById(this.log_id);
	if (logger) {
		logger.value = txt  + "\r\n" + logger.value;
	}
}

You can implement it like:

<textarea id="log_id"></textarea>

<script>

fwdAJAX = new fwd_XMLHTTP('log_id');

var url = 'http://example.com?param=value';
var callback = function(ajax) {
   if (ajax.readyState == 4) {
      alert(ajax.responseText);
   }
}
fwdAJAX.get(url, callback).send();

</script>

see if you get teh same problem.

seems the problem was in:

/*@cc_on @*/
 /*@if (@_jscript_version >= 5)
  try {
   xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
   try {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (E) {
    xmlhttp = false;
   }
  }
 @end @*/

it works when the odd comment-style directives are taken out... as in:

try {
   xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
   try {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (E) {
    xmlhttp = false;
   }
  }

for compatibility/etc i've gone with a shortened version of the following block placed "conditionally-after" the Firefox/pure OO instancing attempts:

var versions = [
		"Msxml2.XMLHTTP.7.0", 
		"Msxml2.XMLHTTP.6.0", 
		"Msxml2.XMLHTTP.5.0", 
		"Msxml2.XMLHTTP.4.0", 
		"MSXML2.XMLHTTP.3.0", 
		"MSXML2.XMLHTTP",
		"Microsoft.XMLHTTP"];
		var n = versions.length;
		for (var i = 0; i <  n; i++) {
			try {
				fwd_XMLHTTP._XMLHTTP_VER = versions[i];
				if (new ActiveXObject(fwd_XMLHTTP._XMLHTTP_VER)) {
					fwd_XMLHTTP.prototype.newXMLHttpRequest = function() {
						return (new ActiveXObject(fwd_XMLHTTP._XMLHTTP_VER));
					};
					this.logger(fwd_XMLHTTP.XML_HTTP_SUPORT_IE+fwd_XMLHTTP._XMLHTTP_VER);
					break;
				}
			} catch (e) {
				// try next version
			}
		}

thanks for the help, it's strange that this syntax:

/*@if (@_jscript_version >= 5)
//do something version specific
@end @*/

works offline but not online o_O

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.