首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery find()可以在Chrome和火狐上运行,但不能在IE上运行

jQuery find()可以在Chrome和火狐上运行,但不能在IE上运行
EN

Stack Overflow用户
提问于 2012-05-20 23:58:13
回答 2查看 944关注 0票数 1

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

代码语言:javascript
复制
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

用户输入一个输入变量是一个表单,点击一个<button>,ajax调用触发并获取上面的数据。然后,根据返回的xml执行相应的操作。这就是我正在做的事情:

代码语言:javascript
复制
<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函数:

代码语言:javascript
复制
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和火狐浏览器中,它可以工作并执行// do some stuff,但在IE中它显示为something is wrong,这意味着find()不能工作。

EN

回答 2

Stack Overflow用户

发布于 2012-05-21 01:29:25

您显式地告诉jQuery将响应视为超文本标记语言而不是可扩展标记语言,因此返回的是一个字符串。然后对该字符串调用$(),这会告诉它将其解析为超文本标记语言,而不是XML。

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

试着这样做:

代码语言:javascript
复制
$.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
);

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

代码语言:javascript
复制
function parseXmlDoi(xmlDoc)
{
    var $xml = $(xmlDoc);

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

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

票数 1
EN

Stack Overflow用户

发布于 2012-05-21 02:22:31

好的,我得到了答案:

我设法从Ajax POST中的url中获得了确切的xml,因此除了header信息之外,响应结果是相同的。

并将我的Ajax调用编辑为:

代码语言:javascript
复制
$.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);
   }   
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10674956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档