jQuery的find()方法返回谷歌浏览器一个空字符串浏览器、方法、空字符串、jQuery

2023-09-10 21:46:21 作者:〣.我没有一百分

我使用jQuery设置一个Ajax请求,抓住从PHP脚本XML提要,然后解析一些信息出来的饲料,并将其插入到DOM。 它工作正常,在Firefox;然而,在Chrome中,我得到了标题元素为空字符串。

下面是Ajax请求的基本设置:

  $。获得(feed.php',函数(oXmlDoc){
  $(oXmlDoc).find('入口')。每个(函数(){
    $(本).find(标题)文本()。
    $(本).find('身份证')文本()。
    //做其他工作?
  });
});
 

有关它的价值,这里的PHP脚本,这与饲料抓取数据。我使用的卷曲,因为我正在做跨域请求(因为它是手头的问题,一个快速和肮脏的解决方案)。

  $卷曲= curl_init();
curl_setopt($卷曲,CURLOPT_URL,$ str_feed_url);
curl_setopt($卷曲,CURLOPT_HEADER,假);
curl_setopt($卷曲,CURLOPT_RETURNTRANSFER,真正的);
$ XML = curl_exec($卷曲);
curl_close($卷曲);

回声$的xml;
 
针对jQuery的5个Chrome浏览器扩展

XML数据被返回正确的,我可以看到的同级节点在Chrome的值(如 ID ),但是,不管是什么原因,我继续得到标题为空字符串节点。

编辑:按照要求,这里的有关XML的一个片段:

 <进入>
    &LT; ID&GT; HTTP://TheAddress.com/feed/01< / ID&GT;
    &LT;标题类型=文本&gt;在后1所述的标题; /标题&GT;
    &LT;笔者&GT;&LT;名称&gt;汤姆和LT; /名称&gt;&LT; /笔者&GT;
    &LT;出版&GT; 2009-11-05T13:46:44Z&LT; /发表&GT;
    &LT;更新&GT; 2009-11-05T14:02:19Z&LT; /更新&GT;
    &LT;摘要类型=HTML&GT; ...&LT; /总结&gt;
&LT; /进入&GT;
 

解决方案

我还没有尝试过,但要确保XML与正确的内容类型(文本/ XML)返回。您还可以设置数据类型设置为XML的 jQuery.ajax(选项)。

I'm using jQuery to setup an Ajax request that grabs an XML feed from a PHP script and then parses some information out of the feed and inserts it into the DOM. It works fine in Firefox; however, in Chrome, I am getting an empty string for the title element.

Here's the basic setup of the Ajax request:

$.get('feed.php', function(oXmlDoc) {
  $(oXmlDoc).find('entry').each(function() {
    $(this).find('title').text();
    $(this).find('id').text();
    // do other work...
  }); 
});

For what it's worth, here's the PHP script that's grabbing data from the feed. I'm using cURL because I'm making the request across domains (and because it was a quick and dirty solution for the problem at hand).

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $str_feed_url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($curl);
curl_close($curl);

echo $xml;

The XML data is being returned correctly and I can see the values of the sibling nodes in Chrome (such as ID), but, for whatever reason, I continue to get an empty string for the title node.

Edit: As requested, here's a fragment of the relevant XML:

<entry>
    <id>http://TheAddress.com/feed/01</id>
    <title type="text">The Title of the Post</title>
    <author><name>Tom</name></author>
    <published>2009-11-05T13:46:44Z</published>
    <updated>2009-11-05T14:02:19Z</updated>
    <summary type="html">...</summary>
</entry>

解决方案

I haven't tried it, but make sure that the xml is returned with the correct content type (text/xml). You can also set the dataType to xml at the jQuery.ajax(options).