-2

I am using jQuery AJAX for my login system.

<script src="http://code.jquery.com/jquery-1.11.3.js" type="text/javascript"></script>
<script src="http://code.jquery.com/jquery.session.js" type="text/javascript"></script>
<script type="text/javascript">
  function Login() {
    var postData = JSON.stringify({
      "username": $("#txtUsername").val(),
      "password": $("#txtPassword").val(),
      "linenumber": $("#ddlLineNum").val()
    });

    $.ajax({
      type: "POST",
      url: "frmLogin.aspx/MyMethod",
      data: postData,
      contentType: "application/json; charset=utf-8",
      success: callbackfunction,
      error: function(msg) {
        alert('ERROR');
      }
    });
  }

  function callbackfunction(result) {
    alert('in call back method');
    if (result.d == "WrongPassword") {
      alert(result.d);
    }

    if (result.d == "Currect Password") {
      alert(username);
      //$.session.set('UserName',username);
      //$.session.set('lineNumber', linenumber);
      //alert($.session.get("UserName"));
      window.location.href = 'Inventory.aspx';
    }
  }
</script>

If I click on my login button for the first time this callbackfunction is not working (alert('in call back method') is not displaying). When I click for the second time the alert is shown.

Can anyone help me out to fix this issue.

if (username == string.Empty)
  return "UserName is Empty";
if (password == string.Empty)
  return "Password is Empty";
if (linenumber == "Please Select")
  return "Line Number is Empty";

DataTable dtLogin = DBLayer.spUserMasterSelect(username);
if (dtLogin.Rows.Count > 0) {
  if (dtLogin.Rows[0]["PASSWORD"].ToString() != password) {
    return "WrongPassword";
  } else {
    return "Currect Password";
  }
} else {
  return "Invalid UserName";
}

REQUEST AND RESPONSE IS frmlogin.aspx header file is Request URL: http://localhost:57075/frmLogin.aspx Request Method: POST Status Code: 200 OK Remote Address: [::1]:57075 Referrer Policy: no-referrer-when-downgrade Cache-Control: private Content-Encoding: gzip Content-Length: 3698 Content-Type: text/html; charset=utf-8 Date: Mon, 09 Sep 2019 09:44:44 GMT Server: Microsoft-IIS/8.0 Vary: Accept-Encoding X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET X-SourceFiles: =?UTF-8?B?RDpcQ3VycmVudCBQcm9qZWN0c1xNVFMtV0NQTVxXQ1BNIE5FVyAtIEpxdWVyeSBhbmQgQWpheFxUQ0lMaVdNU1xmcm1Mb2dpbi5hc3B4?= Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3 Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.9 Cache-Control: max-age=0 Connection: keep-alive Content-Length: 559 Content-Type: application/x-www-form-urlencoded Host: localhost:57075 Origin: http://localhost:57075 Referer: http://localhost:57075/frmLogin.aspx Sec-Fetch-Mode: navigate Sec-Fetch-Site: same-origin Sec-Fetch-User: ?1 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36 __LASTFOCUS: __VIEWSTATE: cGCxGPZDpgAFMuHbrg1A53vAaM0BzvQjz7MfEimIFx+HJKP+qBDaZHi1pnNTxGprZm35gQjfw5qALYaMZBThSWH2j2GnHiOjHU5BnC3D+WluwvbeyevsZenrwCBd06esBm49ZCc5MY+RcTNeiwhGRQ== __VIEWSTATEGENERATOR: 0C51E059 __EVENTTARGET: __EVENTARGUMENT: __EVENTVALIDATION: ikyLG2JZUj1pDNj+/nM4K0teZ/HT96A1W0NQ1y2qhTATegzDSkvfPZrk6PyZo7DF6bQFKOEu+/zf+bTdFHz6UqEnzcMvq+mCZhBQK3Wab92NRtB5w4TllqDq3gbdv0pjeyrXWtmfUexxo76PdXEAoZ1sDBFHhqsdzXMwwX62mFLhmiPoYUmwR1iWxky++B4F+rP4jxQtsvhUnJG8qELgzQ== txtUsername: ADMIN txtPassword: ADMIN ddlLineNum: 1

this is for second time Request URL: http://localhost:57075/frmLogin.aspx Request Method: POST Status Code: 200 OK Remote Address: [::1]:57075 Referrer Policy: no-referrer-when-downgrade Cache-Control: private Content-Encoding: gzip Content-Length: 3708 Content-Type: text/html; charset=utf-8 Date: Mon, 09 Sep 2019 09:46:36 GMT Server: Microsoft-IIS/8.0 Vary: Accept-Encoding X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET X-SourceFiles: =?UTF-8?B?RDpcQ3VycmVudCBQcm9qZWN0c1xNVFMtV0NQTVxXQ1BNIE5FVyAtIEpxdWVyeSBhbmQgQWpheFxUQ0lMaVdNU1xmcm1Mb2dpbi5hc3B4?= Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3 Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.9 Cache-Control: max-age=0 Connection: keep-alive Content-Length: 506 Content-Type: application/x-www-form-urlencoded Host: localhost:57075 Origin: http://localhost:57075 Referer: http://localhost:57075/frmLogin.aspx Sec-Fetch-Mode: navigate Sec-Fetch-Site: same-origin Sec-Fetch-User: ?1 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36 __VIEWSTATE: zG8/UiXsuL3e8xOkmL3OW4uIsVjWX307lugZuGNqFSBzejHQxN86sg9lllvaWrJIJZ6/TRi1lbGpJc9XiurbTZecE0GINMpyzSrrAvaiRmQt8HV1USp55SGw6gF5aVtHtFmkgZe3xFUr06SaXhbiwQ== __VIEWSTATEGENERATOR: 0C51E059 __EVENTVALIDATION: 3rl06ckIZwSoQBBJWRONyuPdNovib37nYRGrZNW1FBCOUf8dsDPSTDUpW8uHMlRTMqd8OPKKXiqaGT/DyI51YJplGnyaoB4E4OuBFX43tL+p4HtbWZf2p8kURyNxUdELR7w1ubkiC0GIkkllT1VqrjmkLeuhnkan7xGgiqjj/V/R++NPQWOD+/B0AM7qJeeheYM2UaLv5ZRuUNc1GnKURg== txtUsername: ADMIN txtPassword: admin ddlLineNum: 1

sudhakar
  • 5
  • 5

1 Answers1

-1

Consider catching a failure from your AJAX post request. It might help troubleshoot for your click on your login button for the first time.

$.ajax({
  type: "POST",
  url: "frmLogin.aspx/MyMethod",
  data: postData,
  contentType: "application/json; charset=utf-8",
  success: callbackfunction,
  failure: failureFxn,
  error: function(msg) {
    alert('ERROR');
  }
});

function failureFxn() {
  console.log('failure');
}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • tried with above code. still no failure . console.log is empty – sudhakar Sep 09 '19 at 10:17
  • There is no `failure` property of `$.ajax`. There is only the `error` callaback which the OP is already using (although it could be used more effectively by checking what `msg` contains). Please read docs: https://api.jquery.com/jquery.ajax/. – Rory McCrossan Sep 09 '19 at 10:18