【问题标题】:jQuery find() does work on Chrome and Firefox but not IEjQuery find() 确实适用于 Chrome 和 Firefox,但不适用于 IE
【发布时间】:2012-05-27 08:18:03
【问题描述】:

这是我从 Web 服务获得的对提交的 Url 的响应。

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/xml;charset=UTF-8
Content-Language: en-US
Transfer-Encoding: chunked
Date: Sun, 20 May 2012 15:35:52 GMT
Connection: close

841
<!--?xml version="1.0" encoding="UTF-8"?-->
<doi_records>
  <doi_record owner="10.1016" timestamp="2012-04-21 12:08:25">
    <crossref>
      <journal>
        <journal_metadata language="en">
          <full_title>Procedia - Social and Behavioral Sciences</full_title>
          <abbrev_title>Procedia - Social and Behavioral Sciences</abbrev_title>
          <issn media_type="print">18770428</issn>
        </journal_metadata>
        <journal_issue>
          <publication_date media_type="print">
            <month>1</month>
            <year>2011</year>
          </publication_date>
          <journal_volume>
            <volume>15</volume>
          </journal_volume>
          <special_numbering>C</special_numbering>
        </journal_issue>
        <journal_article publication_type="full_text">
          <titles>
            <title>The effect of teaching the cognitive and meta-cognitive strategies (self-instruction procedure) on verbal math problem-solving performance of primary school students with verbal problem- solving difficulties</title>
          </titles>
          <contributors>
            <person_name contributor_role="author" sequence="first">
              <given_name>Narges</given_name>
              <surname>Babakhani</surname>
            </person_name>
          </contributors>
          <publication_date media_type="print">
            <month>1</month>
            <year>2011</year>
          </publication_date>
          <pages>
            <first_page>563</first_page>
            <last_page>570</last_page>
          </pages>
          <publisher_item>
            <item_number item_number_type="sequence-number">S1877042811003211</item_number>
            <identifier id_type="pii">S1877042811003211</identifier>
          </publisher_item>
          <doi_data>
            <doi>10.1016/j.sbspro.2011.03.142</doi>
            <resource>http://linkinghub.elsevier.com/retrieve/pii/S1877042811003211</resource>
          </doi_data>
        </journal_article>
      </journal>
    </crossref>
  </doi_record>
</doi_records>
0

用户输入一个输入一个变量是一个表单并点击一个&lt;button&gt;,ajax调用触发并获取上述数据。然后根据返回的 xml 进行适当的操作。 这就是我正在做的:

<script type="text/javascript">
...
if ($('input:text[name=ident]').val() !=  "")
{
    $.post("<?php echo site_url('con/met/') ?>",
    {doi:$('input:text[name=ident]').val()}, 
    function(responseText){ parseXmlDoi(responseText)},  
    "html"
);
...
}
</script>

这是我的 parseXmlDoi 函数:

function parseXmlDoi(xml)
{

    $('#debug').fadeIn();
    $('#debug').html('');

    if ($(xml).find('error').text())
    {
        $('#debug').html('<div dir="rtl" class=\"message warning\"><p>error</p></div>');
    }
    else if ($(xml).find('book').text())
    {
        $('#debug').html('<div dir="rtl" class=\"message info\"><p>this is a book</p></div>');
    }
    else if ($(xml).find('journal').text())
    {

        // do some stuff

    }
    else
    {
         $('#debug').html('<div dir="rtl" class=\"message error\"><p> something is wrong</p></div>');

    }
}

问题:在基于上述 Xml 的 Chrome 和 Firefox 中,它可以工作并执行 // do some stuff,但在 IE 上显示 something is wrong 这意味着 find() 不起作用。

【问题讨论】:

  • 离题,但是:最好不要经常调用$() 来重新解析相同的 XML。调用它一次,然后重用结果。 var $xml = $(xml);...
  • @pna:我们可以在 John 发布的内容中看到内容类型。 text/xml 仍然被识别,即使 application/xml 是首选。但此外,他正在覆盖默认处理。

标签: jquery internet-explorer cross-browser xml-parsing


【解决方案1】:

您明确告诉 jQuery 将响应视为 HTML 而不是 XML,因此您得到的是一个字符串。然后你在那个字符串上调用$(),它告诉它把它解析为HTML,而不是XML。

我看不出覆盖默认处理的任何理由,但我怀疑这可能是问题所在,因为 IE 不太喜欢它不知道的 HTML 标记。

试试这个:

$.post(
    "<?php echo site_url('con/met/') ?>",
    {doi:$('input:text[name=ident]').val()},
    parseXmlDoi,
    "xml" // Or leave this off, your content type should trigger XML handling
);

...并将parseXmlDoi 的开头更改为:

function parseXmlDoi(xmlDoc)
{
    var $xml = $(xmlDoc);

    // ...now, use $xml.find...//
}

通过告诉 jQuery 将响应视为 XML,您将获得一个已解析的 XML 文档,并将其传递给您的成功函数。然后我们使用$() 将该XML 文档包装在一个jQuery 实例中,这样您就可以在其上使用find。

【讨论】:

    【解决方案2】:

    好的,我得到了答案:

    我设法从 Ajax POST 中的 url 获取准确的 xml,所以除了 header 信息之外,响应变成了相同的。

    并编辑了我的 Ajax 调用:

    $.ajax({
     url:"<?php echo site_url('con/met/') ?>",
     type:"POST",
     data:{doi : $('input:text[name=ident]').val()},
     dataType: "html", //this should be html because the php page uses echo to give data back
     success: function(data)
     {
         var xml;
         if (jQuery.browser.msie) { //for IE
           xml = new ActiveXObject("Microsoft.XMLDOM");
           xml.async = false;
           xml.loadXML(data);
         } else { //for non IE
           xml = data;
         }
         parseXmlDoi(xml);
       }   
    });
    

    【讨论】:

    • 这不应该是必要的,并且应该尽可能避免浏览器嗅探。当您尝试我的解决方案(或者您没有尝试)时发生了什么?
    猜你喜欢
    • 2013-04-22
    • 2013-03-24
    • 1970-01-01
    • 2014-07-23
    • 1970-01-01
    • 2019-10-26
    • 2013-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多