我在我们的网站上遇到了最奇怪的行为,这让事情变得异常缓慢。
我和我的团队有一个完全在 AJAX 上运行的网站。所以对于登录,我有一些 js ajax 将登录框加载到我们的索引页面中。包含登录框的 html 在头部有一个脚本链接。该脚本监听登录表单提交,并通过ajax将表单数据发送到服务器进行认证。
包含登录框的 html 只加载一次,但它链接到的 js 文件会加载多次。次数变化。从 5 次到 15 次,我看不到模式或任何东西。这发生在我们网站的任何地方,而不仅仅是在登录时。
这个问题真的让我很难过,我完全被卡住了。是因为我在最初用ajax加载的js文件中有ajax吗?
我非常感谢您的洞察力和帮助!
编辑:
根据要求,一些代码:
这是 Interface.js 文件中 loadContent() 的精简版。此特定功能将所有站点内容加载到 index.php 上的内容区域中。当页面刷新时,首先发送给函数的是 login.php 文件的位置,其中包含登录框:
loadContent: function(page) {
var self = this;
//just some animations to make things look good
$(self.error).fadeOut(150, function() {
$(self.content).fadeOut(150, function() {
$(self.loading).fadeIn(150, function() {
$.ajax({
url: page,
success: function(data) {
//response data
var $response = $(data);
$(self.content_1).html($response);
//definitions for contentbox-2
self.contentHeading_2.html("Replies:");
self.content_2.html(postReplies);
//redisplay the content after it has loaded in.
$(self.loading).fadeOut(150, function() {
$(self.content).fadeIn(150, function() {
// Content faded in
});
});
},
error: function() {
$(self.loading).fadeOut(150, function() {
$(self.error).fadeIn(150, function() {
// Error faded in
});
});
}
});
});
});
});
this.page = page;
}
然后是 login.php 文件:
<!DOCTYPE HTML>
<html>
<head>
<title></title>
<script type="text/javascript" src="js/login.js"></script>
</head>
<body>
<div class="padded loginphp">
<div id="loginbox">
<!-- the login box comes here
</div> <!-- #loginbox -->
</div>
</body>
</html>
和 login.js 文件:
$(document).ready(function() {
$('#honeyloginform').submit(function(event) {
//event.preventDefault();
login();
return false;
});
});
function login() {
$('.errorinputfields').removeClass('errorinputfields');
if (isEmpty($('#username'))) {
$('#username').addClass('errorinputfields');
$('#username').focus();
return;
}
if (isEmpty($('#password'))) {
$('#password').addClass('errorinputfields');
$('#password').focus();
return;
}
$('#honeyloginform').fadeOut(100, function(){
$('#loginbox .loading').fadeIn(300, function(){
var pword = $('#password').val();
var remember = "no";
if ($('#remember').is(':checked')) {
remember = "yes";
}
var JSONobj = {
username: $('#username').val(),
password: pword,
rem: remember
};
$.ajax({
url: 'ajax/login.php',
data: JSONobj,
success: function(data) {
//alert(data);
var JSONobj = JSON.parse(data);
if (JSONobj.Success) {
Interface.login(); //just loads the landing page after login
//window.setTimeout('location.reload()', 300);
} else {
$('#loginbox .loading').fadeOut(300,function(){
$('#honeyloginform').fadeIn(300);
});
$('#username').focus();
$('#loading-message').text(JSONobj.Message).show();
}
}
});
});
});
}