admin管理员组

文章数量:1023838

The javascript I'm using works pletely fine in firefox and ie but has this error when run on chrome and safari. I'm not entirely sure why it's failing.

        var response = asyncResult.value;
        if (window.DOMParser) {
            var parser = new DOMParser();
            xmlDoc = parser.parseFromString(response, "text/xml");

        }
        else 
        {
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = false;
            xmlDoc.loadXML(response);
        }

        console.log(xmlDoc);
         var changeKey = xmlDoc.getElementsById("t:ItemId")[0].getAttribute("ChangeKey");

The console shows this message but outputs the xmlDoc just fine when I have it set to console.log()


Uncaught TypeError: Cannot read property 'getAttribute' of undefined        r.js 
soapToGetItemDataCallback    r.js
r.onreadystatechange    outlookwebapp-15.js:21 
$h.EwsRequest.$1x_1    outlookwebapp-15.js:21 
(anonymous function)   outlookwebapp-15.js:21

The javascript I'm using works pletely fine in firefox and ie but has this error when run on chrome and safari. I'm not entirely sure why it's failing.

        var response = asyncResult.value;
        if (window.DOMParser) {
            var parser = new DOMParser();
            xmlDoc = parser.parseFromString(response, "text/xml");

        }
        else 
        {
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = false;
            xmlDoc.loadXML(response);
        }

        console.log(xmlDoc);
         var changeKey = xmlDoc.getElementsById("t:ItemId")[0].getAttribute("ChangeKey");

The console shows this message but outputs the xmlDoc just fine when I have it set to console.log()


Uncaught TypeError: Cannot read property 'getAttribute' of undefined        r.js 
soapToGetItemDataCallback    r.js
r.onreadystatechange    outlookwebapp-15.js:21 
$h.EwsRequest.$1x_1    outlookwebapp-15.js:21 
(anonymous function)   outlookwebapp-15.js:21
Share Improve this question asked Mar 16, 2015 at 21:17 zoomynnzoomynn 31 gold badge1 silver badge3 bronze badges 6
  • Does your console.log(xmlDoc); output the expected result? – blex Commented Mar 16, 2015 at 21:21
  • yes, in both firefox and chrome – zoomynn Commented Mar 16, 2015 at 21:22
  • getElementsById is an invalid method, so this should fail in all browsers. Perhaps it fails differently in Chrome and Safari than in IE and Firefox. – Rick Hitchcock Commented Mar 16, 2015 at 21:30
  • You're right, I actually copied and pasted the wrong version. At one point, someone suggested I try "byId". I actually originally had getElementsByTagName, which was the source of the original error. Any thoughts ? – zoomynn Commented Mar 16, 2015 at 23:45
  • Does using getElementsByTagName resolve your error? – jasonscript Commented Mar 17, 2015 at 2:10
 |  Show 1 more ment

1 Answer 1

Reset to default 1

The problem is that you are trying to get the element by ID and using [0], I guess you wanna getElementsByTagName because that the result is undefined, the code should be:

var changeKey = xmlDoc.getElementsById("t:ItemId").getAttribute("ChangeKey");

Or if "t:ItemId" is a collection:

 var changeKey = xmlDoc.getElementsByTagName("t:ItemId")[0].getAttribute("ChangeKey");

The javascript I'm using works pletely fine in firefox and ie but has this error when run on chrome and safari. I'm not entirely sure why it's failing.

        var response = asyncResult.value;
        if (window.DOMParser) {
            var parser = new DOMParser();
            xmlDoc = parser.parseFromString(response, "text/xml");

        }
        else 
        {
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = false;
            xmlDoc.loadXML(response);
        }

        console.log(xmlDoc);
         var changeKey = xmlDoc.getElementsById("t:ItemId")[0].getAttribute("ChangeKey");

The console shows this message but outputs the xmlDoc just fine when I have it set to console.log()


Uncaught TypeError: Cannot read property 'getAttribute' of undefined        r.js 
soapToGetItemDataCallback    r.js
r.onreadystatechange    outlookwebapp-15.js:21 
$h.EwsRequest.$1x_1    outlookwebapp-15.js:21 
(anonymous function)   outlookwebapp-15.js:21

The javascript I'm using works pletely fine in firefox and ie but has this error when run on chrome and safari. I'm not entirely sure why it's failing.

        var response = asyncResult.value;
        if (window.DOMParser) {
            var parser = new DOMParser();
            xmlDoc = parser.parseFromString(response, "text/xml");

        }
        else 
        {
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = false;
            xmlDoc.loadXML(response);
        }

        console.log(xmlDoc);
         var changeKey = xmlDoc.getElementsById("t:ItemId")[0].getAttribute("ChangeKey");

The console shows this message but outputs the xmlDoc just fine when I have it set to console.log()


Uncaught TypeError: Cannot read property 'getAttribute' of undefined        r.js 
soapToGetItemDataCallback    r.js
r.onreadystatechange    outlookwebapp-15.js:21 
$h.EwsRequest.$1x_1    outlookwebapp-15.js:21 
(anonymous function)   outlookwebapp-15.js:21
Share Improve this question asked Mar 16, 2015 at 21:17 zoomynnzoomynn 31 gold badge1 silver badge3 bronze badges 6
  • Does your console.log(xmlDoc); output the expected result? – blex Commented Mar 16, 2015 at 21:21
  • yes, in both firefox and chrome – zoomynn Commented Mar 16, 2015 at 21:22
  • getElementsById is an invalid method, so this should fail in all browsers. Perhaps it fails differently in Chrome and Safari than in IE and Firefox. – Rick Hitchcock Commented Mar 16, 2015 at 21:30
  • You're right, I actually copied and pasted the wrong version. At one point, someone suggested I try "byId". I actually originally had getElementsByTagName, which was the source of the original error. Any thoughts ? – zoomynn Commented Mar 16, 2015 at 23:45
  • Does using getElementsByTagName resolve your error? – jasonscript Commented Mar 17, 2015 at 2:10
 |  Show 1 more ment

1 Answer 1

Reset to default 1

The problem is that you are trying to get the element by ID and using [0], I guess you wanna getElementsByTagName because that the result is undefined, the code should be:

var changeKey = xmlDoc.getElementsById("t:ItemId").getAttribute("ChangeKey");

Or if "t:ItemId" is a collection:

 var changeKey = xmlDoc.getElementsByTagName("t:ItemId")[0].getAttribute("ChangeKey");

本文标签: javascriptjquery Cannot read property 39getAttribute39 of undefinedStack Overflow