javascript - AJAX/PHP/JS - 无法将页面内容加载到容器中

标签 javascript php jquery html ajax

我不断收到“没有这样的页面!”每当我尝试使用按钮更改内容时都会发送消息。
这是index.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>eWorld</title>
<link rel="stylesheet" type="text/css" href="/css/style.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="js/script.js"></script>

</head>

<body>

<div id="rounded">
<div id="main" class="container">
<h1>eWORLD</h1>
<h2>You make difference.</h2>

<ul id="navigation">
<li><a href="#forum">Forum</a></li>
<li><a href="#about">About</a></li>
<li><a href="#register">Register</a></li>
<li><a href="#login" target=pageContent>Login</a></li>
<li><img id="loading" src="img/ajax_load.gif" alt="loading" /></li>
</ul>
</div>
<div class="clear"></div>
</div>
<div class="clear"></div>
<div id="pageContent">
</div>
<div class="clear"></div></div>
<div align="center" class="#footer">eWorld</div>

</body>
</html>

script.js:

var default_content="";
$(document).ready(function(){

checkURL();
$('ul li a').click(function (e){

        checkURL(this.hash);

});

default_content = $('#pageContent').html();


setInterval("checkURL()",250);

});

var lasturl="";

function checkURL(hash)
{
if(!hash) hash=window.location.hash;

if(hash != lasturl)
{
    lasturl=hash;

    if(hash=="")
    $('#pageContent').html(default_content);

    else
    loadPage(hash);
}
}


function loadPage(url)
{
url=url.replace('#page','');

$('#loading').css('visibility','visible');

$.ajax({
    type: "POST",
    url: "load_page.php",
    data: 'page='+url,
    dataType: "html",
    success: function(msg){

        if(parseInt(msg)!=0)
        {
            $('#pageContent').html(msg);
            $('#loading').css('visibility','hidden');
        }
    }

});

}

此外,load_page.php:

<?php
if(!$_POST['page']) die("0");

$page = $_POST['page'];

$newstr = str_replace("#","", $page);

if (!file_exists('pages/'.$newstr.'.html'))
echo file_get_contents('pages/'.$newstr.'.html');

else echo 'There is no such page!';
?>

看起来脚本工作正常,但不知何故它无法进入新页面的目录(即/pages/forum.html ;/pages/login.html 等)。 如果有人能告诉我我在哪里犯了错误,我将不胜感激......:)

最佳答案

您正在尝试获取 DOM 元素的 .hash:

$('ul li a').click(function (e){
    checkURL(this.hash);

而是获取 href 属性:

checkURL(this.attr("href"));

.hash 仅适用于 URL 对象

此外,这是 checkURL() 的第一次使用; documentReady 之后应该抛出错误,因为它与方法定义不匹配

关于javascript - AJAX/PHP/JS - 无法将页面内容加载到容器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40807065/

相关文章:

javascript - jquery.show() 不工作

php - 使用命名空间时找不到类

javascript - jquery mobile动态添加元素无法正常工作

javascript - js 并在加载前编辑 url - 如何执行此操作

javascript - css组合器有问题吗?

javascript - 当脚本位于index.html之外时,为什么 Controller 无法工作?

javascript - 如何根据下拉值过滤/呈现项目?

PHP 在字符串中搜索电子邮件地址

php - 获取子帖子 ID 的数组,wordpress 3.0,php

javascript - 使用 Javascript 将输入值作为变量传递