更新时间:2023-12-03 08:36:40
大多数Ajax请求/框架应设置您可以用它来过滤阿贾克斯v非Ajax请求这个特殊的头。我用它来帮助确定在很多项目响应类型(JSON / HTML):
Most Ajax requests/frameworks should set this particular header that you can use to filter Ajax v Non-ajax requests. I use this to help determine response type (json/html) in plenty of projects:
if( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && ( $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' ) )
{
// allow access....
} else {
// ignore....
}
编辑: 您可以在自己的Ajax请求在您的JavaScript code以下添加此自己:
edit: You can add this yourself in your own Ajax requests with the following in your javascript code:
var xhrobj = new XMLHttpRequest();
xhrobj.setRequestHeader("X-Requested-With", "XMLHttpRequest");