php中检测ajax请求,该方法是不安全的,http_x_requested_with是jquery自己加上的

checkajax.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jQuery.get()</title>
<style type="text/css">
ul{border:1px solid black;list-style:none;
margin:0pt;padding:0pt;float:left;
font-family:Verdana,Arial,Helvetica,sans-serif;
font-size:12px;width:300px;}
li{padding:10px 5px;border-bottom:1px solid black;}
</style>
</head>
<body>
<form>
<p>
<input type="button" value="Load Some data" />
</p>
</form>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('input:button').click(function(){
     $.get('checkajax.php',function(data){
          $('input:button').after(data);
     }) ;
});
});

</script>
</body>
</html>
 
 
checkajax.php
<?php
if(isset($_SERVER['HTTP_X_REQUESTED_WITH'])&&$_SERVER['HTTP_X_REQUESTED_WITH']=='XMLHttpRequest'){
     echo "Yall!! Requestsuccessful";
}else{
     echo 'This is not an Ajax request';
}

 

jQuery 文档操作 - after() 方法

实例

在每个 p 元素后插入内容:

$("button").click(function(){
  $("p").after("<p>Hello world!</p>");
});

亲自试一试

定义和用法

after() 方法在被选元素后插入指定的内容。

语法

posted @ 2014-04-04 17:24  wint  Views(256)  Comments(0)    收藏  举报