【问题标题】:AJAX-loaded page gives empty $_GETAJAX 加载的页面给出了空的 $_GET
【发布时间】:2013-03-11 13:51:04
【问题描述】:

通过 AJAX 方法加载的页面。 index.php 是基本页面,profile.php 是加载页面

jQuery: ("post" 与 HTML 请求无关)

$('<a/>', {href: '?userID='+post['userID']+'#profile'}).text(post['firstName']+' '+post['lastName'])

HTML:

<a href="?userID=1#profile">Firstname Lastname</a>

原始网址(点击后)

http://########/#####/index.php?userID=1#home

$_GET print_r on profile.php:

Array ()

根据要求; ajax 加载 javascript (index.php):

    //AJAX page loading

        $(document).ready(function(){

            //Default page
            if(!window.location.hash)
                window.location.hash = '#home';

            //Check page reference
            checkURL();

            //Update nav
            $('#main-nav li').on("click", function (){
                $('#main-nav li').removeClass('active');
                $(this).addClass('active');

                //Assign each link a new onclick event, using their own hash as a parameter
                checkURL(this.hash);    
            });

            //check for a change in the URL every 250 ms to detect if the history buttons have been used
            setInterval("checkURL()",250);  
        });

        //Store the current URL hash
        var lasturl=""; 

        function checkURL(hash){
            if(!hash)

                //if no parameter is provided, use the hash value from the current address
                hash=window.location.hash;
            if(hash != lasturl) { 

                //If hash changed, update current hash and load new one
                lasturl=hash;
                loadPage(hash);
            }

        }

        function loadPage(url) {

            //Adjust page name
            url=url.replace('#','');   
            url=url+'.php';

            //AJAX load page contents in to main content div
            $('#content').load(url);
        }

【问题讨论】:

  • 您在代码的哪个位置加载profile.php?
  • 它在 index.php document.ready 函数中。它很长,但页面加载工作正常(除了上述问题)。
  • 我们需要查看发送ajax请求的部分。
  • 那么你的 URL 看起来像 loadPage 吗?在我看来,您正在删除# 并添加.php,这将给出profile.php。里面没有查询字符串,$_GET`为空是正常的。
  • 这仅适用于 .load() 函数,它需要一个文件指向才能提取其内容。我有一个名为“profile.php”的页面。链接所做的是在片段之前添加?userID=1,本质上是发出服务器请求; index.php?userID=1。我是 AJAX 新手,所以我不确定链接文件 (profile.php) 是否可以访问对基本文件 (index.php) 的 GET 请求……但那怎么可能呢?

标签: php javascript jquery ajax


【解决方案1】:

解决方案:

  1. 在根页面 (index.php) 上创建全局存储。
  2. 在存储中设置变量而不是使用 $_GET。
  3. 从子页面访问变量。

例子:

index.php (Javascript)

if (typeof(window.storage) === 'undefined') {
    window.storagePV = {};
}

$('#nav-profile').click(function() {
    window.storage.userID = <?php echo "$_SESSION[userID]"; ?>;
});

访问用户 ID 以查看用户的个人资料页面

var userID = window.storage.userID

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 2023-03-23
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多