0

我有一个需要使用摘要身份验证验证的表单。我仍在进行身份验证,但我已经布置了基本的 HTML 表单。

<form method="post" id="loginForm" class="validate">

            <div data-role="fieldcontain">
                <label for="password">Email:</label> 
                <input class="required email" type="text" name="username" id="username" placeholder="username@target.com">
            </div>

            <div data-role="fieldcontain"> 
                <label for="password">Password:</label>
                <input class="required" type="password" name="password" id="password" placeholder="password">
            </div>

            <input type="submit" value="Login">

        </form>

我还有一个名为 digest_auth.js 的外部 js 文件,方法是

$('#loginForm').submit(function() {
  alert('click');   
  username = $('#username').value();
  password = $('#password').value();
  realm = tokens['realm'];
  nonce = tokens['nonce'];
  qop = tokens['qop'];
  alert('submitted');
  ha1 = bcrypt(username + ":" + realm + ":" + password);
  ha2 = bcrypt("GET:" + "http://localhost:8090/web/login");
  response = bcrypt(ha1 + ":" + nonce + ":" + nc + ":" + cnonce + ":" + qop + ":" ha2);

  $.ajax(
    type: 'GET', // maybe POST?
    url: url,
    complete: function(xhr, status) {
      if (status == 200) {
        // success. save the nonce and nc in the local storage.
        // whenever you send a request to the server, use the nonce
        // and nc
          alert('Success');
      }
      else {
        // failure, try again
          alert('Failure');
      }
    }
  )
});

但是, .submit 没有被调用。我在开头添加了 alert() 但我什么也没得到。我确实使用链接外部

<script type="text/javascript" src="digest_auth.js"></script>
4

2 回答 2

0

问题在于您的 ajax 请求:

错误:

  1. 响应的串联中缺少“+”
  2. ajax 属性应该在大括号内

用这个:

$(document).ready(function(){
  $('#loginForm').submit(function() {
      alert('click');   
      username = $('#username').value();
      password = $('#password').value();
      realm = tokens['realm'];
      nonce = tokens['nonce'];
      qop = tokens['qop'];
      alert('submitted');
      ha1 = bcrypt(username + ":" + realm + ":" + password);
      ha2 = bcrypt("GET:" + "http://localhost:8090/web/login");
      response = bcrypt(ha1 + ":" + nonce + ":" + nc + ":" + cnonce + ":" + qop + ":" + ha2);

      $.ajax({
        type: 'GET', // maybe POST?
        url: url,
        complete: function(xhr, status) {
          if (status == 200) {
            // success. save the nonce and nc in the local storage.
            // whenever you send a request to the server, use the nonce
            // and nc
              alert('Success');
          }
          else {
            // failure, try again
              alert('Failure');
          }
        }
      })
    });
});
于 2012-12-24T05:23:03.037 回答
0

确保$('#loginForm')在页面中存在表单元素之后执行。

如果该脚本运行并且找不到表单,则提交将触发但不会触发您的函数。

它在这里工作:http: //jsfiddle.net/MxgEf/7/ ​​​​</p>

于 2012-12-24T05:24:14.063 回答