检测的Ajax调用网址 [英] Detect Ajax calling URL

查看:110
本文介绍了检测的Ajax调用网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML文件,它加载的内容使用AJAX调用PHP文件。我的code最重要的一点是如下:

I have an HTML document, which loads content from a PHP file using an AJAX call. The important bit of my code is below:

default.html:

/*more code above*/
var PHP_URL = "content.php";
var Content = document.getElementById('Content');
ajaxRequest = new XMLHttpRequest();
ajaxRequest.onreadystatechange =
    function() {
        if(ajaxRequest.readyState==4) {
            if (ajaxRequest.status==200)
                Content.innerHTML = ajaxRequest.responseText;
            else
                Content.innerHTML = "Error:<br/>unable to load page at <b>"+PHP_URL+"</b>";
            Content.className = "Content Solid";
        }
    }
ajaxRequest.open("GET",PHP_URL,true);
ajaxRequest.send();
/*more code below*/

是否有可能在content.php的文件,以检测它被称为从default.html,或不同的调用文档作为必要吗?

Is it possible for the file at 'content.php' to detect that it has been called from 'default.html', or a different calling document as necessary?

推荐答案

像jQuery和MooTools的最知名的Ajax框架中添加特定的头,你可以检查与PHP的:

Most well-known Ajax frameworks like jQuery and mooTools add a specific header which you can check with PHP:

if (strcasecmp('XMLHttpRequest', $_SERVER['HTTP_X_REQUESTED_WITH']) === 0)
{
    // Ajax Request
}

这篇关于检测的Ajax调用网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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