0

我已经为 php 页面创建了 jquery-ajax 调用,该页面通过根据从第一页接收到的 id 从数据库中选择数据来创建内容

当我单击 person1 时,第一页 index.php 有链接 person1,它通过 jquery 重定向到详细信息页面这里是代码:

 $('.details').click(function() { 
        $("a").removeClass("active");
        $(this).addClass("active"); 
             var id = $(this).attr("id");
              $.ajax({  
                type: "GET",  
                url: "details.php",
                data:{pid:id},
                success: function(html){        
                    $("#persons").html(html).fadeIn(1000);

                }  
            }); 

                 return false;  
        });

内容将在这里加载到 index.php 文件中:

<div class="row">
    <div class="twelve columns" id="persons" style="display:none" >

    </div>
</div>

详情页内容:

   <?

    if(isset($_GET['pid'])){
$id=(int)$_GET['pid'];
    $sql="select id, name,title,details,image,cat from persons  where id ='".$id."' order by name asc";
    $rs=mysql_query($sql)or die(mysql_error());
    if(list($id,$name,$title,$details,$image,$cat)=mysql_fetch_array($rs)){
  ?>
    <h3><? echo $name;?></h3>
    <h5><? echo $title;?></h5>
    <p align="justify">
        <img alt="<? echo $name;?>" src="images/persons/<? echo $image?>" title="<? echo $name;?>" style="float:right; margin-left:15px;" />
        <? echo $details;?>
    </p>
    <?
    }
    }
?>

如何检查页面是否已通过 ajax 调用或普通 php 调用请求?如果不是ajax调用,是否正常加载详情页内容?

4

1 回答 1

0

您可以检查以下变量:

$_SERVER['HTTP_X_REQUESTED_WITH']

对于 AJAX 请求,它应该是“xmlhttprequest”。

但是,您可以简单地向 URL 添加一个参数:

url: "details.php?thisisanajaxrequest=1",

只需检查 URL 变量。

于 2012-07-31T00:42:01.647 回答