简体   繁体   English

阿贾克斯电话工作; 不再工作

[英]Ajax Call Worked; Doesn't Work Anymore

I have an AJAX call on a page for administrators to e-sign. 我在页面上有一个AJAX呼叫供管理员进行电子签名。 When a button (adminEsign_btn) is pressed, this jQuery is called: 当按下按钮(adminEsign_btn)时,此jQuery称为:

$("#adminEsign_btn").click(function(e){//admin esign submitted
  e.preventDefault();
  //validate esign
  var valid = true;
  if($.trim($('#adminSignature').val()).length < 3){
     valid = false;
  }
  //action
  if(valid === false){
     $('#adminEsignError').html('<span class="error">You must agree to the statement and sign.</span>');
  }
  else{//validation passed, submit data
     var schoolID = <?php echo $schoolProfile['schoolID']; ?>;
     var signature = $('#adminSignature').val();
     $('#adminEsignLoader').css({'display':'inline-block'});
     $('#submitForm').attr("disabled","disabled");
     $('#submitForm').val("Updating...");
     $.ajax({
        type: "POST",
        url: 'bin/schoolProfile.saveEsign.php',
        data: { schoolID:schoolID, signature:signature}
     }).done(function(response) {
        //$('#debug').html(response);
        //alert(response);
        if(response.indexOf('success') >= 0){//if 'success' exists in the response text
           $('#submitForm').removeAttr("disabled");
           $('#submitForm').val("Update School Profile");
           $('#adminEsignLoader').hide();
           //disable the e-sign
           $('#adminAgree').attr("disabled","disabled");
           $('#adminSignature').attr("readonly","readonly");
           $('#adminEsign_btn').attr("disabled","disabled");
        }
     });
     $('#adminEsignError').html('');
  }
});

I didn't write the original code, so I don't know exactly what is going on in the if statement: 我没有编写原始代码,所以我不知道if语句到底发生了什么:

if(response.indexOf('success') >= 0){//if 'success' exists in the response text

But the call isn't expecting a return other than an echo of success. 但是,除了成功的呼声外,电话会议并不期望回报。 The following php page (schoolProfile.saveEsign.php) is what is called: 以下php页面(schoolProfile.saveEsign.php)称为:

<?php
include($_SERVER['DOCUMENT_ROOT'] . '/includes/init.php');
$Page->clearance('Admin');

$Main->saveSchoolAdminEsign($_POST['schoolID'], $_POST['signature']);

echo 'success';
?>

NOTE: $Main is initialized in init.php as well as is $Page. 注意:$ Main以及$ Page都在init.php中初始化。

This code worked up until today when I tested it again. 直到今天我再次对其进行测试时,该代码一直有效。 It is supposed to post the data to schoolProfile.saveEsign.php and run a function to save the schoolID and signature to a mysql database. 应该将数据发布到schoolProfile.saveEsign.php并运行一个将schoolID和签名保存到mysql数据库的函数。

I've used the javascript alert() function to post the results of the "response" and it always shows the code for the entire current page (schoolProfile.edit.php). 我已经使用javascript alert()函数发布了“响应”的结果,它始终显示整个当前页面的代码(schoolProfile.edit.php)。

I've run the code in Chrome and it shows that the data is being posted. 我已经在Chrome中运行了代码,它显示了数据正在发布。 It has status 302 - Found. 状态为302-找到。 The size is 485 B (sounds reasonable for 2 variables with only text), but underneath size in Chrome Network Debugger is content and content is 0 B - empty. 大小为485 B(听起来很合理,只有2个变量为纯文本),但Chrome Network Debugger中的大小在下面,内容为content,内容为0 B-空。 I don't know if this means the data isn't being sent or what. 我不知道这是否意味着没有发送数据或什么。

I've tested setting a Session variable to see if the session gets saved and I haven't been able to change it's value so that may be a sign that the data isn't actually being pushed across. 我已经测试过设置一个Session变量,以查看是否保存了该会话,并且我无法更改其值,因此这可能表明该数据实际上并未被推送。 But when I view the header for the page being called, it shows the 2 variables - schoolID and signature - with values. 但是,当我查看被调用页面的标题时,它显示了两个带有值的变量-schoolID和signature。

I'm new to Chrome Network Debugger so if there are any other things I can check or if anyone has any suggestions any help would be appreciated. 我是Chrome Network Debugger的新手,所以如果我可以检查其他任何事情,或者任何人有任何建议,我们将不胜感激。

EDIT: I've also tested the success and error functions inside the ajax call and success is always called. 编辑:我也测试了ajax调用内的成功和错误函数,并且总是调用成功。 Once again it only returns the entire code for the current page (schoolProfile.edit.php). 再次,它仅返回当前页面的整个代码(schoolProfile.edit.php)。

I found the issue. 我发现了问题。 In my include($_SERVER['DOCUMENT_ROOT'] . '/includes/init.php'); 在我的include($_SERVER['DOCUMENT_ROOT'] . '/includes/init.php'); The init.php document redirects the user to schoolProfile.edit.php if they haven't completed filling out the school profile and it also makes sure that they aren't already at that url using PHP's $_SERVER['REQUEST_URI'] . 如果用户尚未完成填写学校资料,则init.php文档会将用户重定向到schoolProfile.edit.php,并且使用PHP的$_SERVER['REQUEST_URI']来确保用户尚未到达该网址。

The issue was that when trying to call schoolProfile.saveEsign.php, this url was not in the "list" of okay'd URL's so the AJAX request was always being redirected to schoolProfile.edit.php - AKA the current page. 问题是,当尝试调用schoolProfile.saveEsign.php时,此URL不在正常URL的“列表”中,因此AJAX请求始终被重定向到schoolProfile.edit.php-也就是当前页面。 That is why I would always see the current page code when I would do an alert. 这就是为什么我发出警报时总是看到当前页面代码的原因。

For future reference for myself. 供我将来参考。 Original code: 原始代码:

if($_SERVER['REQUEST_URI'] != '/settings/schoolProfile.edit?f='.$Main->encrypt("INCOMPLETE")) {
   header("Location:/settings/schoolProfile.edit?f=".$Main->encrypt("INCOMPLETE"));
   exit();
}

Fixed Code: 固定代码:

if($_SERVER['REQUEST_URI'] != '/settings/schoolProfile.edit?f='.$Main->encrypt("INCOMPLETE")
   && $_SERVER['REQUEST_URI'] != '/settings/bin/schoolProfile.saveEsign'
   && $_SERVER['REQUEST_URI'] != '/settings/bin/schoolProfile.saveEsign.php') {
  header("Location:/settings/schoolProfile.edit?f=".$Main->encrypt("INCOMPLETE"));
  exit();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM