2013-01-24 187 views
-1

我有一个AJAX帖子,发布网站到PHP,然后返回它,我使用jQuery来查找和返回元素文本。这是工作形式的一些元素,但不是全部,我不明白为什么。对于body元标题和标题标记,它将返回undefined。是我的PHP或AJAX职位?jquery .html返回undefined

的jQuery/AJAX

var dataString = name; 
     //alert (dataString);return false; 

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

$(response).find('p').each(function() { 
    $('body').append("<p> contents of p: " + $(this).html() + "</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

你是否使用Firebug的控制台来观察请求/ repsonse? –

回答

4

你的AJAX调用期待JSON数据,以响应该请求,所以在你的success回调response变量将是一些物体类。您无法将常规对象传递给jQuery函数,但它不是为了处理它而设计的。

像使用其他任何物体一样使用对象,使用点 - object.property - 或方括号 - object["property"] - 表示法。或者更改您的服务器端代码,以便返回可用格式(HTML或XML)而不是JSON。

+0

我将ajax文章中的数据类型更改为html,并将php echo json_encode($ html)更改为echo($ html),但其仍然无效。我只开始学习PHP,所以还是不太了解它。 – user2002077