2012-12-24 2 views
0

다이제스트 인증을 사용하여 유효성을 검사해야하는 양식이 있습니다. 나는 여전히 인증 작업을하고 있지만 기본적인 HTML 폼이있다. 양식 확인 JQuery .submit()

<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="[email protected]"> 
      </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> 

은 또한 방법 그러나, .submit가 호출되지 않는

$('#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'); 
     } 
    } 
) 
}); 

와 JS 외장라는 파일 digest_auth.js 있습니다. 처음에는 알림()을 추가했지만 아무 것도 얻지 못했습니다. 나는

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

이 자바 스크립트가 DOM 후로드 http://jsfiddle.net/MxgEf/7/은 (body 태그에있는 스크립트)? 그렇지 않다면'$ (function() {... 여기에 코드 ..}); jquery closuer (onready function) – bokonic

+0

어떻게 ajax를위한 "url"을 만들고 있습니까? – ATOzTOA

답변

0

와 외부를 연결 않았다 문제는 아약스 요청입니다 :

오류 :

  1. 는 "+"
  2. 아약스 속성이 내부해야 응답을 연쇄 실종 중괄호

다음을 사용하십시오.

$(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'); 
      } 
     } 
     }) 
    }); 
}); 
+0

인라인이 작동하지만 스크립트가 HTML에 불필요하게 연결됩니다. 그는 요소가 DOM에 있기 전에 요소를 선택하기 만합니다 (스크립트가 'onready'이후가 아니라 ''에 있기 때문에). – bokonic

+0

@bokonic 업데이트 된 답변 ... – ATOzTOA

+0

여전히 작동하지 않습니다 :/ –

0

페이지에 양식 요소가있는 후에 $('#loginForm')이 실행되는지 확인하십시오.

해당 스크립트가 실행되고 제출보다 형식을 찾지 못하면 트리거하지만 트리거하지는 않습니다.

이 여기서 일하는 :

+0

DOM 요소 란 무엇입니까? –

+0

양식이 존재하면 죄송합니다. 해당 스크립트가 실행되고 제출보다 양식이 없으면 트리거하지만 사용자의 기능은 트리거되지 않습니다. – Scott

+0

그래서 내 HTML doc $ (document) .ready (function() {})에서 이들 중 하나가 필요합니다; ? –