2013-01-24 45 views
0

我有一個ajax發送一個網站地址到php它檢索它,然後我想jQuery找到網站內的某些元素,並檢索這些元素中的文本。它適用於像h1 h2 p a等一些元素,但不適用於所有網站,我無法獲得正文文本和元標籤,即body.text不會返回任何內容。是我的阿賈克斯後或PHP導致問題在這裏?jquery not returns body.text

這裏是我的ajax後

var dataString = name; 

      $.ajax({ 
     type: "POST", 
     url: "senddom.php", 
     data: {"dataString" : dataString }, 
     dataType: "json", 
     success: function(response) { 
    $('body').append("<p> contents of title:" + $(response).find("Title").text()+ "</p>");   
    $('body').append("<p> contents of meta:" + $(response).find('Meta').text()+ "</p>");  
    $('body').append("<p> contents of all: " + $(response).find('body').text() + "</p>"); 

$(response).find('p').each(function() { 
    $('body').append("<p> contents of p: " + $(this).text() + "</p>"); 
}); 

和我的PHP,我只是開始學習

<?php 
    $site= $_POST['dataString'];    // get data 
function curl_get($site){ 
    $useragent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)'; 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL,$site); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5); 
    curl_setopt($ch, CURLOPT_USERAGENT, $useragent); 
    $data=curl_exec($ch); 
    curl_close($ch); 
    return $data; 
} 

function getdom($site){ 
    $html = curl_get($site); 
    // Create a new DOM Document 
    $xml = new DOMDocument(); 
    @$xml->loadHTML($html); 
    echo json_encode($html); 
} 
echo getdom($site); 
?> 
+0

你可以添加一個響應樣本結構嗎? – Wolf

+0

你是指當我運行它時輸出的內容? – user2002077

+0

是的。這將有助於稱號 – Wolf

回答

0

jQuery的文檔,我發現了一些interessing。 我們不能在腳本上使用.text()方法。所以我很確定,你的腳本在你的身上,所以.text()不起作用。 使用.html(),它應該工作。

http://api.jquery.com/text/

+0

謝謝你我之前嘗試過,當我這樣做時,它會出現在身體旁邊undefined – user2002077