Webpages on Firefox works well but no on IE - javascript

I have following code snippet:
self.xmlHttpReq = new XMLHttpRequest();
self.xmlHttpReq.onreadystatechange = function()
{
if(self.xmlHttpReq.readyState == 4 && self.xmlHttpReq.status == 200)
{
xmlDoc = self.xmlHttpReq.responseXML;
var xmlVar1 = xmlDoc.getElementsByTagName('var1')[0].childNodes[0].nodeValue;
var xmlVar2 = xmlDoc.getElementsByTagName('var2')[0].childNodes[0].nodeValue;
}
}
In IE the error code says:
object required, ajax request.js line num, char num
However, this same ajax request works fine in Firefox.

IE and Firefox have different object names for the XMLHttpRequest, you have to check your browser and declare the new object based on that.
Try something like this:
function getXHR() {
var xhr = false;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
xhr = new ActiveXObject("msxml2.XMLHTTP");
} catch(e) {
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
xhr = false;
}
}
}
return xhr;
}
I got this from Jeremy Keith some time ago, it has never failed me.

Internet Explorer doesn't have the XMLHttpRequest object. Instead it uses an ActiveX object for the same functionality. So, you need to change this line:
self.xmlHttpReq = new XMLHttpRequest();
to:
if (window.ActiveXObject) {
try {
self.xmlHttpReq = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (e) {
self.xmlHttpReq = new ActiveXObject('Msxml2.XMLHTTP'); // for really old versions of IE. You can leave the try/catch out if you don't care to support browsers from the '90s.
}
}
else
self.xmlHttpReq = new XMLHttpRequest();

Related

XHR object creation

What difference between:
var xmlhttp = getXmlHttp()
and
var xmlhttp = new XMLHttpRequest()
?
If I correctly understand, each of this two cases create XRH object.
Please look at this function:
function getXMLHttp() {
var x = false;
try {
x = new XMLHttpRequest();
}
catch(e) {
try {
x = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(ex) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1) {
x = false;
}
}
}
return x;
}
getXMLHttp() is your custom function to simplify the creation an XHR object with the cross browser issues.
XMLHttpRequest is an object to used with the current modern browser. For the old browser like IE5 or IE6, you can use ActiveXObject("Microsoft.XMLHTTP");
However, the return object is the same for each browser.
If you open up Chrome Developer tools and try the following:
> getXmlHttp()
ReferenceError: getXmlHttp is not defined
This indicates that getXmlHttp is not a built in function.

Javascript: InvalidStateError:DOM Exception 11

I have below code that handling the HTTP request. But I am getting
Error: InvalidStateError:DOM Exception 11
error.
if (window.XMLHttpRequest) {
req_settings = new XMLHttpRequest();
req_settings.onreadystatechange = processChange;
req_settings.open("GET", url, true);
req_settings.send();
} else if (window.ActiveXObject) {
req_settings = new ActiveXObject("Microsoft.XMLHTTP");
if (req_settings) {
req_settings.onreadystatechange = processChange;
req_settings.open("GET", url, true);
req_settings.send();
}
}
req_settings.onreadystatechange = processChange;
req_settings.send();
Please help.
You're calling send() twice, which is invalid. Your code should be
if (window.XMLHttpRequest) {
var req_settings = new XMLHttpRequest();
} else if (window.ActiveXObject) {
req_settings = new ActiveXObject("Microsoft.XMLHTTP");
} else
throw "environment does not support ajax";
req_settings.onreadystatechange = processChange;
req_settings.open("GET", url, true);
req_settings.send();
The code makes no sense, you would be calling
req_settings.onreadystatechange = processChange;
req_settings.send();
twice, Does it inside the if and outside of it! Remove the ones inside. Also it should be using the native object. Use a library!

AJAX Uncaught reference error

Script.js:
var request = new XMLHttprequest();
request.open('GET','data.txt',false);
if(request.status===200) {
console.log(request);
document.writeln(request.responseText);
}
This is my javascript file. I am getting this error:
Uncaught reference error:XMLHttprequest is not defined
Please help.
Sincere thanks.
I tried this it's simple mistake,
var request = new XMLHttpRequest();
dont use the simple letter for xmlHttpRequest. It should be a XMLHttpRequest. Also your simple r should be a capital R. it's work for me.Also try a different version of browser.
This line:
var request = new XMLHttprequest();
Should be:
var request = new XMLHttpRequest();
//^ Capital 'R'
Case in JavaScript, like most languages, matters
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");//this is for ie
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");//this is for ie
} catch (E) {
try{
xmlhttp =new XMLHttpRequest();//for browsers other than ie
}
catch(e)
{
}
}
For browsers like ie XMLHttpRequest doesnt work
Try the following method to get your XML HTTP Request:
function GetXmlHttpObject()
{
try {
var xmlHttp = null;
if (window.XMLHttpRequest)
{
// If IE7, Mozilla, Safari, etc: Use native object
xmlHttp = new XMLHttpRequest()
}
else
{
if (window.ActiveXObject)
{
// ...otherwise, use the ActiveX control for IE5.x and IE6
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
} catch(e)
{
alert(e.message);
}
}

SCRIPT 600: Invalid target element for this operation. (IE Only)

I have a ajax calendar I am working on, it is working fine in Chrome, Safari & Firefox as usual. However It is not working in IE9 or below.
I am getting the following error SCRIPT 600: Invalid target element for this operation.
It is a WP plugin, but this is the code
function show_micro_ajax(response) {
document.getElementById('wp-calendar').innerHTML = response;
}
function microAjax(url, cF) {
this.bF = function(caller, object) {
return function() {
return caller.apply(object, new Array(object));
}
};
this.sC = function(object) {
if (this.r.readyState == 4) {
this.cF(this.r.responseText);
}
};
this.gR = function() {
if (window.ActiveXObject) return new ActiveXObject('Microsoft.XMLHTTP');
else if (window.XMLHttpRequest) return new XMLHttpRequest();
else return false;
};
if (arguments[2]) this.pb = arguments[2];
else this.pb = "";
this.cF = cF;
this.url = url;
this.r = this.gR();
if (this.r) {
this.r.onreadystatechange = this.bF(this.sC, this);
if (this.pb != "") {
this.r.open("POST", url, true);
this.r.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
this.r.setRequestHeader('Connection', 'close');
} else {
this.r.open("GET", url, true);
}
this.r.send(this.pb);
}
}​
If you do not have to support IE6, remove the ActiveX line,
otherwise, reverse your test:
if (window.XMLHttpRequest) return new XMLHttpRequest();
else if (window.ActiveXObject) return new ActiveXObject('Microsoft.XMLHTTP');
else return false;
I'm not sure how the instance becomes an array here,
and possibly IE doesn't either, especially if one of the members in sn ActiveX object.
return caller.apply(object, new Array(object))

XML ActiveXObject IE. Object doesn't support this action

I am trying to work with xml and javascript. In firefox it works great using XMLHttpRequest but in IE (6-8) I am getting the error:
Object doesn't support this action
I am using the following function:
function createRequestObject(){
var request;
try {
request = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = false;
}
}
}
return request;
}
And then calling it with:
var xhttp = createRequestObject();
xhttp.open("GET","myfile.xml",false);
...
Any thoughts??
Try specifying a local variable for request, var request ( although it doesn't look like it should solve it ).
I would use this fn for light-weight XHR:
/** XHConn - Simple XMLHTTP Interface - bfults#gmail.com - 2005-04-08 **
** Code licensed under Creative Commons Attribution-ShareAlike License **
** http://creativecommons.org/licenses/by-sa/2.0/ **/
function XHConn()
{
var xmlhttp, bComplete = false;
try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
catch (e) { try { xmlhttp = new XMLHttpRequest(); }
catch (e) { xmlhttp = false; }}}
if (!xmlhttp) return null;
this.connect = function(sURL, sMethod, sVars, fnDone)
{
if (!xmlhttp) return false;
bComplete = false;
sMethod = sMethod.toUpperCase();
try {
if (sMethod == "GET")
{
xmlhttp.open(sMethod, sURL+"?"+sVars, true);
sVars = "";
}
else
{
xmlhttp.open(sMethod, sURL, true);
xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
xmlhttp.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
}
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && !bComplete)
{
bComplete = true;
fnDone(xmlhttp);
}};
xmlhttp.send(sVars);
}
catch(z) { return false; }
return true;
};
return this;
}
Usage:
var myConn = new XHConn();
if (!myConn) alert("XMLHTTP not available. Try a newer/better browser.");
var fnWhenDone = function (oXML) { alert(oXML.responseText); };
myConn.connect("mypage.php", "POST", "foo=bar&baz=qux", fnWhenDone);
I think you need to put a var in front of request:
function createRequestObject(){
var request;
try {
request = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = false;
}
}
}
return request;
}
IE often has problems with global variables.

Categories

Resources