Home  >  Article  >  php教程  >  PHP中检测ajax请求的代码例子

PHP中检测ajax请求的代码例子

WBOY
WBOYOriginal
2016-06-13 09:37:19749browse

   多数情况下,基于JavaScript 的Js框架如jquery、Mootools、Prototype等,在发出Ajax请求指令时,都会发送额外的 HTTP_X_REQUESTED_WITH 头部信息,就当是一个ajax请求,因此你可以在服务器端检测到这些Ajax请求,在PHP中的检测方法如下代码所示:

  1

  2if(!emptyempty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) =='xmlhttprequest'){

  3 //这里面是你的ajax请求处理逻辑

  4}else{

  5 //一些逻辑。。。

  6}

  7?>

  在开发Ajax交互应用时候用得到。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn