【问题标题】:xml selectNodes not supported using jquery ajax call使用 jquery ajax 调用不支持 xml selectNodes
【发布时间】:2014-02-13 01:34:36
【问题描述】:

我的任务是更新一个最初编写为只能在 IE 中运行的网站,并使其能够跨浏览器运行。

我现在收到一条错误消息:

对象不支持属性或方法'selectNodes'

在从 IE 特定的 Ajax 调用更新为应该跨浏览器工作的 jquery ajax 调用之后的 ajax 调用的输出:

这是调用的原始函数:

function XmlHttpRequestBlocking(url, http_type, return_xml, content) {

var http_request = new ActiveXObject('Microsoft.XMLHTTP');    
if(http_request == undefined)
    return undefined;
try
{ 
    http_request.open(http_type, url, false);    
    http_request.send(content); 

    if(http_request.status == 403)
    {
        top.location = "Login.aspx";
        return undefined;
    }
    else if (http_request.status == 200) 
    {
        if (return_xml) {
            return http_request.responseXML;
        }
        else {
            return http_request.responseText; 
        }
    }
    else
        return undefined;
}
catch(e)
{
    return undefined;
}

}

然后替换为:

function XmlHttpRequestBlocking(url, http_type, return_xml, content) {

var x;

jQuery.ajax({
    url: url,
    type: 'GET',
    async: false,
    success: function (result) {
        x = result;

    }
});

return x;

}

现在对函数的以下调用并尝试解析输出会在 IE 中产生错误“对象不支持属性或方法'selectNodes'”

        var capture_station_list = XmlHttpRequestBlocking(g_get_status_url, "GET", true, "");

        var stations = capture_station_list.selectNodes("//channel");

我在想返回的xml格式一定不一样吧?尝试 console.log 的 jquery 版本的输出显示 [object Document] 返回。

【问题讨论】:

    标签: jquery ajax xml internet-explorer


    【解决方案1】:

    这可能是怎么回事:

    在原始代码中,该函数返回一个XML DOM object。在新代码中,该函数返回一个 jQuery XML 文档。 jQuery 对象没有 selectNodes 方法。一种方法是重写 XML 的解析,以便它使用 jQuery。 on the jquery parseXML documentation page 有一个简单的例子。在您的情况下,它可能类似于:

      var capture_station_list = XmlHttpRequestBlocking(...);   
      $(capture_station_list).find("channel").each(function(i, station) {
          console.log(station);
          ...
      });
    

    【讨论】:

      猜你喜欢
      • 2023-04-05
      • 2012-04-19
      • 2015-08-26
      • 1970-01-01
      • 1970-01-01
      • 2011-12-16
      • 2013-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多