-1

ウェブサイトをphpに投稿するajax投稿があり、それを返します.jqueryを使用して要素テキストを見つけて返します。これは一部の要素で機能していますが、すべてではありません。その理由はわかりません。body メタタグとタイトルタグの場合、未定義が返されます。それは私のphpまたはajaxの投稿ですか?

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);
?>
4

1 に答える 1

4

AJAX 呼び出しは、リクエストに応じて JSON データを想定しているため、コールバックのresponse変数successは何らかのオブジェクトになります。通常のオブジェクトを jQuery 関数に渡すことはできません。それを処理するように設計されていません。

object.propertyドット-- または角括弧 --object["property"]表記を使用して、他のオブジェクトと同じようにオブジェクトを使用します。または、サーバー側のコードを変更して、JSON ではなく使用可能な形式 (HTML または XML) を返すようにします。

于 2013-01-24T20:55:44.693 に答える