当前位置: 移动技术网 > IT编程>脚本编程>Ajax > 判断请求头中是否含有某属性来判断是否是ajax请求

判断请求头中是否含有某属性来判断是否是ajax请求

2017年12月12日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:

<html>
<head>
<script language="javascript">

function cl()
{
var xmlhttp;

if (window.xmlhttprequest)
{
xmlhttp=new xmlhttprequest();
}
else
{
xmlhttp=new activexobject("microsoft.xmlhttp");
}


xmlhttp.open("post","ajax2.html",true);
xmlhttp.setrequestheader ("content-type","application/x- www-form-urlencoded");
//jquery yui默认会发送一个含有http_x_requested_with的http请求头消息,因此,可以通过判断请求头中是否含有该属性来判断是否时ajax请求
xmlhttp.setrequestheader('http_x_requested_with', 'http_x_requested_with');
xmlhttp.send();

xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readystate==4 && xmlhttp.status==200)
{
alert(xmlhttp.responsetext);
}
}
}

</script>
</head>
<body>
<input type="button" id="bt" name="bt" value="ok" onclick="cl();" />
</body>
</html>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网