使用AJAX无法从HTML返回

时间:2014-04-19 20:14:03

标签: php jquery html ajax

我正在尝试使用AJAX获取数据。我的代码如下。基本上我要做的是:第一个元素包含HTML,第二个元素包含另一个JSON数组,其中包含数据。 第二个元素找不到,但第一个元素没有返回任何结果。 如果我写这样的代码

$html = $cont[0][0];

当我编写像这样的代码时,它会返回结果

$html = '<div>This is Html</div>';

现在我真的不知道为什么会这样。如果我echo第一个code样式,它会显示数据,但没有通过它。

脚本代码

<script>
function get_data_from_rul() { //alert('ting');
    var post_url = jQuery('#prod_url').val();
     //alert('hmm');    
    jQuery.ajax({
      url: 'ajax.php',
      type: 'post',
      data: {'post_url': post_url},
      dataType: "json",
      success: function(serverResponse) {
          //location.reload();
          //console.log(data);
          jQuery('#prod_detail').val(serverResponse.html);
          var data = JSON.parse(serverResponse.data);


          jQuery('#meta_description').val(data.description);
          jQuery('#meta_keyword').val(data.keywords);
          jQuery('#prod_title').val(data.page_title);

      }


    });
}
</script>

HTML代码:

<input type="text" id="prod_url" name="prod_url"  class="large" onkeyup="get_data_from_rul();" />
<br />
<textarea id="meta_description" rows="8" cols="90"></textarea><br />
<textarea id="meta_keyword" rows="8" cols="90"></textarea><br />
<textarea id="prod_title" rows="8" cols="90"></textarea><br />
<textarea id="prod_detail" rows="8" cols="90"></textarea>

ajax.php代码:

<?php

    $url = $_REQUEST['post_url'];

function get_web_page( $url )
{
    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_USERAGENT      => "spider", // who am i
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    );

    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;
    return $header;
}

$result = get_web_page( $url );

preg_match('/<title>(.+)<\/title>/',$result['content'],$matches[]);
preg_match('/<div id="specs-list">([^`]*?)<\/div>/',$result['content'],$cont[]);

$tags = get_meta_tags($url);

$keywords       = $tags['keywords'];
$description    = $tags['description'];
$page_title     = $matches[0][1];

$html = $cont[0][0];
$data = json_encode(array('keywords'=>$keywords,'description'=>$description,'page_title'=>$page_title));
$response = array('html'=>$html, 'data'=>$data);
echo json_encode($response);

1 个答案:

答案 0 :(得分:0)

为什么您在json_encode$data中调用了两次$response?您只能在$response拨打json_encode

你确定你应该在数组$cont[0][0]的第一个位置,而不是$cont[0]吗?

preg_match('/<div id="specs-list">([^`]*?)<\/div>/',$result['content'],$cont); // Remove this [] braces, it is not neccessary, on code after you should check array

部分:

$tags = get_meta_tags($url);

$keywords       = $tags['keywords'];
$description    = $tags['description'];
$page_title     = $matches[0][1];

if (isset($cont[0][0])
{
   $html = $cont[0][0];
   // echo($cont[0][0]); // Uncomment to check if you really have results.
}

// If you are not sure of returned result check with print_r and find array
// var_dump(isset($cont[0][1])); ....

$data = (array('keywords'=>$keywords,'description'=>$description,'page_title'=>$page_title));
$response = array('html'=>$html, 'data'=>$data);
echo json_encode($response);

不确定,但希望这有帮助。