【问题标题】:Jquery qtip ajax issueJquery qtip ajax问题
【发布时间】:2010-03-24 12:18:48
【问题描述】:

我正在尝试将输入框的值(在本例中为 imdb 链接)发布到我的 imdbgrabber.php 页面,并让它将该电影的信息返回到 qtip 框中。

编辑:查看此处http://movieo.no-ip.org/ 将图像悬停,您将看到错误。

在我尝试将变量发布到 imdbgrabber 页面之前,一切正常。这是代码。

Javascript:

 var link = $("#link").val();
    var imdbLink = 'link='+ link;

$(".moviebox").qtip({
   style: { name: 'cream' },
   content: {
     method: 'GET',
     data: imdbLink,
     url: '/includes/imdbgrabber.php',
     text: '<img class="throbber" src="/images/loading.gif" alt="Loading..." />'
   },
   position: {
         corner: {
           target: 'bottomright',
          tooltip: 'bottomleft'
        }
      }
});

HTML:

 <!--start moviebox-->
  <div class="moviebox">
  <a href="#">
  <img src="http://1.bp.blogspot.com/_mySxtRcQIag/S6deHcoChaI/AAAAAAAAObc/Z1Xg3aB_wkU/s200/rising_sun.jpg" />
  <form method="get" action="">
              <input type="text" name="link" id="link" style="display:none" value="http://www.imdb.com/title/tt0367882"/>
 </form>
  </a>
  </div>
  <!--end moviebox-->

最后是php:

<?php

$url=$_GET['link'];

//$url = 'http://www.imdb.com/title/tt0367882/';

//get the page content
$imdb_content = get_data($url);

//parse for product name
$name = get_match('/<title>(.*)<\/title>/isU',$imdb_content);
$director = strip_tags(get_match('/<h5[^>]*>Director:<\/h5>(.*)<\/div>/isU',$imdb_content));
$plot = get_match('/<h5[^>]*>Plot:<\/h5>(.*)<\/div>/isU',$imdb_content);
$release_date = get_match('/<h5[^>]*>Release Date:<\/h5>(.*)<\/div>/isU',$imdb_content);
$mpaa = get_match('/<a href="\/mpaa">MPAA<\/a>:<\/h5>(.*)<\/div>/isU',$imdb_content);
$run_time = get_match('/Runtime:<\/h5>(.*)<\/div>/isU',$imdb_content);
$rating = get_match('/<div class="starbar-meta">(.*)<\/div>/isU',$imdb_content);

////build content
//$content = '<h2>Film</h2><p>'.$name.'</p>'
//          . '<h2>Director</h2><p>'.$director.'</p>'
//          . '<h2>Plot</h2><p>'.substr($plot,0,strpos($plot,'<a')).'</p>'
//          . '<h2>Release Date</h2><p>'.substr($release_date,0,strpos($release_date,'<a')).'</p>'
//          . '<h2>MPAA</h2><p>'.$mpaa.'</p>'
//          . '<h2>Run Time</h2><p>'.$run_time.'</p>'
//          . '<h2>Full Details</h2><p><a href="'.$url.'" rel="nofollow">'.$url.'</a></p>';



//gets the match content
function get_match($regex,$content)
{
    preg_match($regex,$content,$matches);
    return $matches[1];
}

//gets the data from a URL
function get_data($url)
{
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
?>

  <!--start infobox-->
    <div class="info"> 
  <span>
  <?php echo '<strong>'.$name.'</strong>' ?>
  </span>

  <img src="http://1.bp.blogspot.com/_mySxtRcQIag/S6deHcoChaI/AAAAAAAAObc/Z1Xg3aB_wkU/s200/rising_sun.jpg" /> 
  <div class="plot">
  <?php echo ''.substr($plot,0,strpos($plot,'<a')).'</div>' ?>
  </div>

  <div class="runtime">
  <?php echo'<strong>Run Time</strong><br />'.$run_time.'</div>' ?>
  </div>
<div class="releasedate">
<?php echo '<strong>Release Date</strong><br />'.substr($release_date,0,strpos($release_date,'<a')).'</div>' ?>
</div>
<div class="director">
<?php echo '<strong>Director</strong><br />'.$director.'' ?>
</div>
  <div class="rating">
  <?php echo '<strong>Rating</strong><br />'.$rating.'' ?>
  </div>
  </div>
  <!--end infobox-->

我确信这是某个地方的一个简单错误,但经过几个小时的研究,我想我会问专家。

【问题讨论】:

    标签: php jquery ajax post get


    【解决方案1】:

    根据文档,通过 AJAX 获取时 qtip 的数据需要是键值对。试试这个:

    var link = $("#link").val();
    var imdbLink =  { "link" : link };  // note change to data...
    
    $(".moviebox").qtip({
       style: { name: 'cream' },
       content: {
         method: 'GET',
         data: imdbLink,   // you could make this { "link" : link }
         url: '/includes/imdbgrabber.php',
         text: '<img class="throbber" src="/images/loading.gif" alt="Loading..." />'
       },
       position: {
             corner: {
               target: 'bottomright',
              tooltip: 'bottomleft'
            }
          }
    });
    

    此外,您的 get_match 例程似乎未正确索引到匹配数组中。数组是从零开始的,所以第一个匹配是索引 0 处的匹配,而不是一个。事实上,给定正则表达式时,您只能有一个匹配项,因为您只有一个分组表达式。

    尝试将其更改为:

    //gets the match content  
    function get_match($regex,$content)  
    {  
        preg_match($regex,$content,$matches);  
        return $matches[0];
    }
    

    【讨论】:

    • 同样的问题在这里检查:movieo.no-ip.org。悬停图片看看会发生什么
    • 我明白了。问题不在于请求,而在于您的代码。会更新。
    • 改变了同样的问题。 ajax 发布的信息很好,所以这是某个地方的 php 的问题
    • 至少您不再收到 PHP 错误,因此索引问题得到了解决。现在的问题似乎是数据显示不正确,是吗?
    • 您当然知道,出于商业目的从 IMDB 获取数据而不与他们签订许可协议是非法的,对吧?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    • 2010-11-13
    • 1970-01-01
    相关资源
    最近更新 更多