prevent整个PHP执行的Ajax请求 [英] Prevent entire php execution on ajax request

查看:83
本文介绍了prevent整个PHP执行的Ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AJAX脚本(jQuery的),要求index.php来输出生成的变量。此的index.php也是其中产生整页,最初填充数据的文件。我怎么能只让getNewData()函数时,Ajax请求此文件。现在,它复制了整个页面,并输出其所以有是二的一切。

I have an ajax script (jquery) that asks index.php to output generated variables. This index.php is also the file where the whole page is generated and initially populated with data. How can I only let the getNewData() function when ajax requests this file. Right now it duplicates the entire page and outputs it so there is two of everything.

推荐答案

本机PHP实现Dooltaz的回答会是如下:

A native PHP implementation of Dooltaz's answer would be as follows:

function isAjax() {
    return $_SERVER['HTTP_X_REQUESTED_WITH'] === "XMLHttpRequest";
}

当然,

这是偶然的 X_Requested_With 头居然被送到了客户XHR请求。据我所知,所有主要的JavaScript库做包括这个头,但如果你滚动你的XHR实现,你需要添加它。

This of course, is contingent on the X_Requested_With header actually being sent by the clients XHR request. As far as I know, all major Javascript libraries do include this header, but if you're rolling on your XHR implementation, you'll need to add it.

这篇关于prevent整个PHP执行的Ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆