我尝试了很多解决方案;我用 FireBug 调试过,这个错误没有意义。我制作了与此类似的其他页面,它们可以完美运行。标头发送正确,FireBug 中显示的响应实际上是正确的响应文本;但它只是没有显示在页面上!相反,它返回“内部服务器错误 500”,我不明白为什么。
这是我的页面:
第一个是我处理新页面创建的类页面,这样您就可以看到如何引用 javascript 脚本:
<?php
// Page Class
class Page {
public $title;
function __construct ( $pageTitle = '', $scripts = "" )
{
global $config;
if ( strlen ( $pageTitle ) > 0 )
{
$this->title = $config['site']['name'] . ' » ' . $pageTitle;
$usePTitle = 1;
} else {
$this->title = $config['site']['name'];
$usePTitle = 0;
}
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' . "\n",
'<html xmlns="http://www.w3.org/1999/xhtml" lang="en">' . "\n",
'<head>' . "\n",
' <title>' . $this->title . '</title>' . "\n",
' <link rel="stylesheet" type="text/css" href="default.css" />' . "\n",
' <script type="text/javascript" src="scripts/jquery.js"></script>' . "\n",
' <script type="text/javascript" src="scripts/functions.js"></script>' . "\n";
if ( strlen ( $scripts ) > 0 )
{
echo ' <script type="text/javascript">' . "\n",
' <!--' . "\n";
include 'scripts/' . $scripts;
echo ' //-->' . "\n",
' </script>' . "\n";
}
echo '</head>' . "\n",
'<body>' . "\n",
' <h2>' . ( $usePTitle == 1 ? $pageTitle : $config['site']['name'] ) . '</h2>' . "\n",
'<hr class="nv" />' . "\n",
$this->head();
}
function head ( )
{
global $user, $sql, $config;
if ( $user->auth == true )
{
$pmc = $sql->result ( $sql->query ( 'SELECT COUNT(*) FROM `private_msgs` WHERE `pm_user` = ' . intval ( $user->info['id'] ) ) );
$headLinks = array ( 'Control Panel' => 'user.php',
'Private Messages (' . $pmc . ')' => 'messages.php',
'Logout' => 'logout.php',
'Contact Us' => 'contact.php'
);
echo ' <div id="userbar">' . "\n",
$user->info('name') . ' (' . $user->info('level') . ') ' . "\n",
' <ul id="userlinks">' . "\n";
foreach ( $headLinks as $t => $l )
{
echo ' <li><a href="' . $l . '">' . $t . '</a></li>' . "\n";
}
echo ' </ul>' . "\n",
' </div>' . "\n",
'<hr class="nv" />' . "\n";
} else {
$headLinks = array ( 'Register' => 'register.php',
'Login' => 'login.php',
'Contact Us' => 'contact.php'
);
echo ' <div id="userbar">' . "\n",
' Guest (' . $config['levels']['guest'] . ') ' . "\n",
' <ul id="userlinks">' . "\n";
foreach ( $headLinks as $t => $l )
{
echo ' <li><a href="' . $l . '">' . $t . '</a></li>' . "\n";
}
echo ' </ul>' . "\n",
' </div>' . "\n",
'<hr class="nv" />' . "\n";
}
}
function restrict ( $lvl )
{
global $user;
if ( $user->info('level') < $lvl )
{
echo ' <h2>Authorization Error</h2>' . "\n",
' <div class="text">' . "\n",
' You are not authorized to view this page.' . "\n",
' </div>' . "\n",
'<hr class="nv" />' . "\n";
$page->genFooter(1);
}
}
function genFooter ( $die = 0 )
{
$year = date ( 'Y', time() );
$copy = '';
echo ' <div id="footer">' . "\n",
' ©' . $year . ' ' . $copy . "\n",
' </div>' . "\n",
'</body>' . "\n",
'</html>';
if ( $die == 1 )
die();
}
}
?>
下一页是我应该运行登录的实际 login.php 页面:
<?php
require 'inc.common.php';
$page = new Page ( 'Login', 'login.js' );
?>
<div id="content">
<div id="result" class="result"></div>
<h2>Login</h2>
<div class="text">
<fieldset>
<fieldset>
<legend>Username</legend>
<input type="text" id="un" value="" size="20" /> <span id="un_error" class="error">*</span>
</fieldset>
<fieldset>
<legend>Password</legend>
<input type="password" id="pw" value="" size="30" /> <span id="pw_error" class="error">*</span>
</fieldset>
<input type="button" id="login" value="Login" />
</fieldset>
</div>
</div>
</body>
</html>
下一页是我的“login.js”页面,其中包含 JavaScript/AJAX 代码:
$(document).ready(function() {
$("#login").click(function() {
document.getElementById("result").innerHTML = 'Validating login...';
var user = $("#un").val();
var pass = $("#pw").val();
if ( user == "" )
{
document.getElementById("un_error").style.visibility = 'visible';
$("#un").focus();
}
if ( pass == "" )
{
document.getElementById("pw_error").style.visibility = 'visible';
$("#pw").focus();
}
$.ajax({
type: "POST",
url: "http://stjohnspikapp.org/ajaxforums/login-parse.php",
data: "un=" + user + "&pw=" + pass + "&session=" + Math.random(),
success: function(msg) { document.getElementById("result").innerHTML = msg; }
});
});
});
最后一页是 login-parse.php 页面,它接收 AJAX 请求并发送响应...
<?php
require 'inc.common.php';
$uname = $_POST['un'];
$pword = $_POST['pw'];
$errors = array();
if (! strlen($uname) )
$errors[] = 'Invalid username.';
if (! strlen($pword) )
$errors[] = 'Invalid password.';
$check = $sql->query('SELECT * FROM `users` WHERE `user_name` = \'' . $sql->escape($uname) . '\' AND `user_password` = \'' . $sql->escape(md5($pword)) . '\'' );
if ( $sql->num_rows ( $check ) == 0 )
$errors[] = 'Invalid username/password combination.';
/*if ( count ( $errors ) > 0 )
{
echo '<blockquote>The following errors occurred while processing your request:' . "\n",
'<ul>' . "\n";
foreach ( $errors as $enum => $e )
{
echo ' <li><strong>(#' . ($enum+1) . '):</strong> ' . $e . '</li>' . "\n";
}
echo '</ul>' . "\n",
'</blockquote>' . "\n";*/
if ( count ( $errors ) > 0 )
{
echo '<blockquote>There was an error with your submission.</blockquote>' . "\n";
} else {
setcookie ( 'ajax_un', $uname, time()+60*3600 );
setcookie ( 'ajax_pw', $pword, time()+60*3600 );
echo '<blockquote>' . "\n",
' You have been logged in successfully!<br />' . "\n",
' You may now return to the <a href="index.php">board index</a>.' . "\n",
'</blockquote>' . "\n";
}
?>