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

前端技术 2023/09/08 AJAX
复制代码 代码如下:

<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>

本文地址:https://www.stayed.cn/item/22791

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。