2012-05-20 52 views
1

这是我从一个提交的Url的Web服务中得到的回应。jQuery find()可以在Chrome和Firefox上使用,但不能使用IE

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应该完成相应的操作。 这是我在做什么:

<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>'); 

    } 
} 

问题:在Chrome和Firefox基于上面的XML给出的,它的工作原理和它执行// do some stuff但IE说它something is wrong这意味着find()不起作用。

+0

题外话,但:最好不要经常叫'$()'重新解析相同的XML。调用*一次*然后重新使用结果。 'var $ xml = $(xml);'... –

+0

检查了这一点http://stackoverflow.com/questions/562283/jquery-find-doesnt-return-data-in-ie-but-does-in- firefox-and-chrome – pna

+0

@pna:我们可以看到John发布的内容类型。尽管'application/xml'是首选的,但仍然可以识别'text/xml'。但是,他正在重写默认处理。 –

回答

1

您明确告诉jQuery将响应视为HTML而不是XML,因此您返回的是字符串。然后你在该字符串上调用$(),告诉它将它解析为而不是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

0

确定我得到了答案:

我设法从url确切XML在阿贾克斯POST所以响应转作除了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); 
    } 
}); 
+0

这不应该是必要的,并且尽可能避免浏览器嗅探。当你尝试我的解决方案时发生了什么(或者你没有尝试过)? –

相关问题