我有一个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);
?>
你可以添加一个响应样本结构吗? – Wolf
你是指当我运行它时输出的内容? – user2002077
是的。这将有助于称号 – Wolf