【问题标题】:GetElementbyId from XMLHttpRequest() in IE 8.0IE 8.0 中 XMLHttpRequest() 中的 GetElementbyId
【发布时间】:2014-02-03 10:57:22
【问题描述】:

我正在开发 youtube 用户历史记录查看功能,现在我正在尝试使用 youtubeInfoUri 和请求的令牌获取 YouTube 用户 ID,

这里的代码在 FF 和 Chrome 中运行良好,但在 IE 中失败

function getDataFromXML() {
try {

    youTubeUserInfoUri += acToken; // global variable currently. 
    // i.e youTubeUserInfoUri = "https://gdata...."
    //IE : if URI contain https  then exception thrown  :
    /* description : $lineinfo is undefined */
    /* Name : TypeeEror */        

    //To handle this :
    youTubeUserInfoUri = youTubeUserInfoUri.replace("https", "http");

    var request = new XMLHttpRequest();

    request.open("GET", youTubeUserInfoUri, false);


    request.timeout = 3000;
    request.onprogress = function () { };
    request.ontimeout = function () { };
    request.onerror = function () { };


    request.send();

    if (request.status == 200) {

        var xml = request.responseXML;     

在 xml 中:我有 childnodes = {count 0}; item =无效的参数个数

现在我完全空白,不知道为什么会发生这种情况..

        if (xml == null) {
            //if Google account dont have youtube account / error 401 found ,
            showShortNotification("YouTube", "current user is not linked with YouTube", "error");
        } else {

            //Browser exception handle :  
            //TagName understanding is different by FF and & Chrome browser,

            var users = xml.getElementsByTagName("username"); // here check for Chrome:   Firefox will not understand this tag name
            if (users.length == 0) { // if its length is 0 means no elements found, so try for next tag method
                users = xml.getElementsByTagName("yt:username"); // here firefox understand this method


          /* In IE : during debug point : here users = Item : Invalid number of parameters.*/


            }

            for (var i = 0; i < users.length; i++) {
                var youtubeUserid = (users[i].childNodes[0].nodeValue);
                YoutubeUserHistoryFetch(youtubeUserid);
            }
        }
    }
} catch (e) {
    alert("Error found" + e);

   }
 }

我也试过 [1] 新窗口.XDomainRequest(); [2] 尝试更改 Internet Explorer 受信任站点区域设置(Internet Explorer 选项->安全->受信任站点 + 自定义级别,向下滚动并将跨域访问数据源值更改为启用

Xml 响应文本:

 .....<yt:maxUploadDuration seconds="930" /> 
 <yt:statistics lastWebAccess="1970-01-01T00:00:00.000Z" subscriberCount="9" videoWatchCount="0" viewCount="0" totalUploadViews="70" /> 
  <media:thumbnail url="http://yt4.ggpht.com/-pFKkUesgV-Q/AAAAAAAAAAI/AAAAAAAAAAA/I23mZGfUo-A/s88-c-k-no/photo.jpg" /> 
  <yt:username>saun4frsh</yt:username> 

我需要

如果您有任何解决此问题的想法,请提供帮助。 只有 Internet Explorer 的问题,相同的代码在 Chrome 和 FF 中运行良好。

我有 IE8,win 7 操作系统。

【问题讨论】:

  • 不要使用同步请求。
  • 如果我使用 >> request.open("GET", youTubeUserInfoUri);而不是 request.open("GET", youTubeUserInfoUri,false) 我得到错误 ::: responseText="完成此操作所需的数据尚不可用"

标签: javascript internet-explorer xmlhttprequest youtube-javascript-api


【解决方案1】:

使用此代码:特别适用于 IE 8,100% 工作

function getDataFromXML() {
try {
    youTubeUserInfoUri += acToken;
    if (navigator.userAgent.search("MSIE") >= 0) {
        var rssData = httpGet();
        var youtubeUserid = convertToArrayForIE(rssData.getElementsByTagName('yt:username'));
        YoutubeUserHistoryFetch(youtubeUserid[0]);

    } else {
    //Rest ur code for FF/Chrome Old code
  }
}
catch{}
}

function httpGet() {

youTubeUserInfoUri = youTubeUserInfoUri.replace("https", "http"); //IE not allows https : security issue.
var xmlHttp = null;

xmlHttp = new XMLHttpRequest();
xmlHttp.open('GET', youTubeUserInfoUri, false);
xmlHttp.send();

if (window.DOMParser) {
    var parser = new DOMParser();
    var doc = parser.parseFromString(xmlHttp.responseText, 'text/xml');
    return doc;
}
else {
    var xmlDocument = new ActiveXObject('Microsoft.XMLDOM');
    xmlDocument.async = false;
    xmlDocument.loadXML(xmlHttp.responseText);
    return xmlDocument;
   }
}

function convertToArrayForIE(htmlCollection) {
var nodes = [];
var collectionLength = htmlCollection.length;
for (var i = 0; i < collectionLength; i++) {
    nodes.push(htmlCollection.item(i).firstChild.text);
}
return nodes;
}

【讨论】:

    猜你喜欢
    • 2010-10-08
    • 2012-07-13
    • 2010-09-09
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-17
    • 1970-01-01
    相关资源
    最近更新 更多