title: Javascript/AJAX sends multiple returns
body: <p>I'm trying to replace a table row once a user enters his/her credentials and the credentials are validated.  It's a simple AJAX call to a servlet, sending the credentials and returning some specific information (first name, last name, zip code, etc).  </p>
  
  <p>I noticed that the call to authenticate actually returns multiple times, the first couple being null and "undefined".  I put some code in to handle this, and then removed all my trouble-shooting alerts, and noticed that it wasn't working at all.</p>
  
  <p><strong>With the alert just prior to the authenticate() return in place, it works as I intend.  If I take the alert out, however, it stops working.</strong></p>
  
  <p>I haven't used AJAX that much, and can't define the problem enough to do any research online.  I'm hoping that someone will recognize my issue despite my poor attempt at describing the problem.</p>
  
  <p>So, where am I going wrong on this?</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Training Daily Activity Report&lt;/title&gt;
      &lt;meta charset="UTF-8"&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
      &lt;link rel="stylesheet" type="text/css" href="stylesheets/dar.css"&gt;
      &lt;script src="js/DAR.js"&gt;&lt;/script&gt;
      &lt;script src="js/jquery-1.10.2.min.js"&gt;&lt;/script&gt;
      &lt;style type='text/css'&gt;
          html { 
              background: url(images/officebuilding.jpg) no-repeat center center fixed; 
              -webkit-background-size: cover;
              -moz-background-size: cover;
              -o-background-size: cover;
              background-size: cover;
          }
      &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div style='width: 100%;text-align: center;margin-top: 55px;'&gt;
          &lt;div style='width: 90%;margin-right: auto; margin-left: auto;'&gt;
              &lt;form id='fmDAR' action='DARServlet' method='post'&gt;
                  &lt;input type="hidden" id="formType" name="formType" value="" /&gt;
              &lt;/form&gt;
              &lt;table style='width: 100%'&gt;
                  &lt;tr&gt;
                      &lt;td colspan='2' class='firstBlock'&gt;
                          &lt;span style='font-size: 24px'&gt;Daily Activity Report&lt;/span&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td style='width: 15%;text-align: right;'&gt;
                          &lt;span style='padding-right: 5px;'&gt;Categories worked on:&lt;/span&gt;
                      &lt;/td&gt;
                      &lt;td style='width: 75%;'&gt;
                          &lt;textarea rows='3' style='width: 99%;' type='text' id='taCategories' name='taCategories' value=''&gt;&lt;/textarea&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
              &lt;br&gt;
              &lt;table style='width: 100%;'&gt;
                  &lt;tr&gt;
                      &lt;td class="secondBlock"&gt;
                          &lt;span&gt;Trainee&lt;/span&gt;
                      &lt;/td&gt;
                      &lt;td class="secondBlock"&gt;
                          &lt;span&gt;DTO/Trainer&lt;/span&gt;
                      &lt;/td&gt;
                      &lt;td class="secondBlock"&gt;
                          &lt;span&gt;Phase&lt;/span&gt;
                      &lt;/td&gt;
                      &lt;td class="secondBlock"&gt;
                          &lt;span&gt;DOT&lt;/span&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td style='text-align: center;'&gt;
                          &lt;input style="width: 98%;" type="text" id="txtTrainee" name="txtTrainee" value="" /&gt;
                      &lt;/td&gt;
                      &lt;td style='text-align: center;'&gt;
                          &lt;input style='width: 98%;' type="text" id="txtTrainee" name="txtTrainee" value="" /&gt;
                      &lt;/td&gt;
                      &lt;td style='text-align: center;'&gt;
                          &lt;input style='width: 98%;' type="text" id="txtTrainee" name="txtTrainee" value="" /&gt;
                      &lt;/td&gt;
                      &lt;td style='text-align: center;'&gt;
                          &lt;input style='width: 98%;' type="text" id="txtTrainee" name="txtTrainee" value="" /&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
              &lt;br&gt;
              &lt;table style='width: 100%;'&gt;
                  &lt;tr&gt;
                      &lt;td class='thirdBlock'&gt;
                          Trainee's Comments:  What did you learn?  What do you still need to know?
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td style='width: 100%;'&gt;
                          &lt;textarea style='width: 99%;' rows='8' id='taTraineeComments' name='taTraineeComments' value=''&gt;&lt;/textarea&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
              &lt;br&gt;
              &lt;table style='width: 100%;'&gt;
                  &lt;tr&gt;
                      &lt;td class='fourthBlock'&gt;
                          DTO Comments:  What areas of trainee performance need improvement?
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td style='width: 100%;'&gt;
                          &lt;textarea style='width: 99%;' rows='8' id='taDTOComments' name='taDTOComments' value=''&gt;&lt;/textarea&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
              &lt;br&gt;
              &lt;table style='width: 100%;'&gt;
                  &lt;thead&gt;
                      &lt;tr&gt;
                          &lt;td&gt;&amp;nbsp;&lt;/td&gt;
                          &lt;td&gt;Logon ID&lt;/td&gt;
                          &lt;td&gt;Password&lt;/td&gt;
                          &lt;td&gt;&amp;nbsp;&lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;/thead&gt;
                  &lt;tbody&gt;
                      &lt;tr&gt;
                          &lt;td class='authentication' id='tdTraineeAuthentication'&gt;
                              Trainee Authentication:
                          &lt;/td&gt;
                          &lt;td&gt;
                              &lt;input type='text' id='txtTraineeAuthLogon' name='txtTraineeAuthLogon' value='' /&gt;
                          &lt;/td&gt;
                          &lt;td&gt;
                              &lt;input type='password' id='txtTraineeAuthPW' name='txtTraineeAuthPW' value='' /&gt;
                          &lt;/td&gt;
                          &lt;td&gt;
                              &lt;button onclick='authenticateUser()'&gt;Authenticate&lt;/button&gt;
                          &lt;/td&gt;
                      &lt;/tr&gt;
                      &lt;tr&gt;
                          &lt;td class='authentication'&gt;
                              DTO Authentication:
                          &lt;/td&gt;
                          &lt;td&gt;
                              &lt;input type='text' id='txtDTOAuthLogon' name='txtDTOAuthLogon' value='' /&gt;
                          &lt;/td&gt;
                          &lt;td&gt;
                              &lt;input type='password' id='txtDTOAuthPW' name='txtDTOAuthPW' value='' /&gt;
                          &lt;/td&gt;
                          &lt;td&gt;
                              &lt;button onclick='authenticateDTO()'&gt;Authenticate&lt;/button&gt;
                          &lt;/td&gt;
                      &lt;/tr&gt;
                      &lt;tr&gt;
                          &lt;td class='authentication'&gt;
                              DTS Authentication:
                          &lt;/td&gt;
                          &lt;td&gt;
                              &lt;input type='text' id='txtDTSAuthLogon' name='txtDTSAuthLogon' value='' /&gt;
                          &lt;/td&gt;
                          &lt;td&gt;
                              &lt;input type='password' id='txtDTSAuthPW' name='txtDTSAuthPW' value='' /&gt;
                          &lt;/td&gt;
                          &lt;td&gt;
                              &lt;button onclick='authenticateDTS()'&gt;Authenticate&lt;/button&gt;
                          &lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;/tbody&gt;
              &lt;/table&gt;
          &lt;/div&gt;
          &lt;div style='text-align: center;margin-top: 20px;'&gt;
              &lt;button style='height: 45px;' id='btnSave' onclick='saveDAR()'&gt;Save Daily Activity Report&lt;/button&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  
  &lt;/body&gt;
  </code></pre>
  
  <p></p>
  
  <p>JS:</p>
  
  <pre><code>function authenticateUser() {
      var logon = document.getElementById("txtTraineeAuthLogon").value;
      var pw = document.getElementById("txtTraineeAuthPW").value;
      if (logon != null &amp;&amp; pw != null) {
          var tmp = authenticate(logon, pw);
          var tmp2;
          if (tmp != null) { //authenticate returns several results, the first few being "null" or "undefined"
              if (tmp.toLowerCase() != 'undefined') {
                  tmp2 = tmp;
              }
          }
          if (tmp2 != null) {
              var tmpRow = "                    &lt;tr&gt;"
                      + "                        &lt;td class='authentication' id='trTraineeAuthentication'&gt;"
                      + "                            Trainee Authentication:"
                      + "                        &lt;/td&gt;"
                      + "                        &lt;td colspan='3' style=\"text-align: center;\"&gt;"
                      + "                            Authenticated as " + tmp2 + ""
                      + "                        &lt;/td&gt;"
                      + "                    &lt;/tr&gt;";
              $("td#tdTraineeAuthentication").parent().replaceWith(tmpRow);
          }
      }
  }
  function authenticateDTO() {
  }
  function authenticateDTS() {
  }
  function authenticate(logon, password) {
      var returnString = null;
      var givenName;
      var sn;
      var postalCode;
      $.ajax({
          url: 'DARServlet',
          data: {
              formType: 'authenticate', logon: logon, password: password
          },
          contentType: 'application/json',
          mimeType: 'application/json',
          success: function (responseText) {
              $.each(responseText, function (key, value) {
                  if (key == "givenName") {
                      givenName = value;
                  } else if (key == "sn") {
                      sn = value;
                  } else if (key == "postalCode") {
                      postalCode = value;
                  }
              });
              returnString = givenName + " " + sn + ", Employee #: " + postalCode;
              //alert(returnString);
          },
          error: function (request, status, error) {
              alert("The credentials used cannot be authenticated.  Please re-enter and try again.");
              return null;
          }
      });
      alert("returnString: " + returnString);
      return returnString;
  }
  </code></pre>