Detecting an Ajax request in PHP

1:index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
    .btn{ border: 1px green solid; background-color:green;border-radius: 3px; width: 200px; height:30px;}
    </style>
    <script type="text/javascript" src="../../js/jquery.js"></script>
    <script type="text/javascript">
    $(function(){
        $('input:button').click(function(){
        $.get(
            'check.php',
            function(data){
            $('#info').html(data);        
            }
        );    
        });    
    }); 
    </script>
</head>
<body>
    <h1>Detecing Ajax Request</h1>
    <input type="button" value="Load Some data" class="btn"/>
    <div id="info"></div>
</body>
</html>

2:check.php

<?php
    if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
    $_SERVER['HTTP_X_REQUESTED_WITH']=='XMLHttpRequest'){
    echo 'Request Successful!';    
    }else{
    echo 'This is not an ajax request!';    
    }
?>

 

posted @ 2013-10-25 17:37  yshy  阅读(185)  评论(0编辑  收藏  举报