简体   繁体   中英

Ajax Call Worked; Doesn't Work Anymore

I have an AJAX call on a page for administrators to e-sign. When a button (adminEsign_btn) is pressed, this jQuery is called:

$("#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(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
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.

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.

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).

I've run the code in Chrome and it shows that the data is being posted. It has status 302 - Found. 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. 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. But when I view the header for the page being called, it shows the 2 variables - schoolID and signature - with values.

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.

EDIT: I've also tested the success and error functions inside the ajax call and success is always called. Once again it only returns the entire code for the current page (schoolProfile.edit.php).

I found the issue. In my 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'] .

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. 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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