dougua2309 2013-01-24 20:52
浏览 258

jquery .html返回undefined

I have ajax post which posts website to php which then returns it and I am using jquery to find and return element text. This is working form some elements but not all and I dont understand why. For the body meta and title tags it is returning undefined. Is it my php or ajax post?

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>");
});

my 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);
?>
  • 写回答

1条回答 默认 最新

  • doutong4088 2013-01-24 20:55
    关注

    Your AJAX call is expecting JSON data in response to the request, so the response variable in your success callback will be an object of some kind. You can't pass a regular object to the jQuery function, it's not designed to handle it.

    Use the object as you would any other, using either the dot - object.property - or square bracket - object["property"] - notations. Or change your server-side code so it returns a usable format (HTML or XML) instead of JSON.

    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制