2016-07-17 5 views
0

내가 XMLHttpRequest를 사용하여 다른 페이지에서 값을 가져 오기 위해 노력하고 내가 모든 경고는 다음 스크립트에서 닫힌 후 값 만 가져온 것으로 나타났습니다 :XMLHttpRequest의 값을 가져 오는하지

function changePw_prompt() { 
    var success = false; 
    while(!success) { 
     var newPassword = prompt("Please enter a new password", ""); 
     if (newPassword != null) { 
      replyMsg = sendPassword(newPassword); 
      if (replyMsg == "CHNGSUCC") { 
       alert("Password changed"); 
       sucess = true; 
      } else if (replyMsg == "CHNGFAIL") { 
       alert("Error: Password is shorter than 8 characters."); 
       sucess = false; 
      } else if (replyMsg == "CHNGERROR") { 
       alert("Internal error, please contact server admin."); 
       sucess = true; 
      } 
     } else { 
      alert("Password unchanged"); 
      success = true; 
     } 
    } 

} 

function sendPassword(pwd) { 
    alert("Run 2nd function"); 
    var xmlhttp2 = new XMLHttpRequest(); 
    xmlhttp2.onreadystatechange = function() { 
     if (xmlhttp2.readyState == 4 && xmlhttp2.status == 200) { 
      var reply2 = xmlhttp2.responseText; 
      alert(reply2); 
      return reply2; 
     } 
    } 
    xmlhttp2.open("POST","changepw.php",true); 
    xmlhttp2.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
    xmlhttp2.send("uid="+document.getElementById("amsnr").value+"&pwd="+pwd); 
} 

나는 무엇이 잘못되었는지 알고 싶습니다. 또한 다른 페이지의 응답을 가져 오는 replyMsg 변수에 대해 올바른 값을 반환하려면 코드를 어떻게 변경해야합니까?

감사합니다.

업데이트가 더 간단하도록 코드를 수정했지만 여전히 작동하지 않습니다. 코드는 다음과 같습니다. 콜백 함수를 사용하여 본래의 목적을 유지하려면 코드를 어떻게 수정해야합니까?

function changePw_prompt() { 
    var success = false; 
    while(!success) { 
     var replyMsg = ""; 
     var newPassword = prompt("Please enter a new password", ""); 
     if (newPassword != null) { 
      var xmlhttp2 = new XMLHttpRequest(); 
      xmlhttp2.onreadystatechange = function() { 
       if (xmlhttp2.readyState == 4 && xmlhttp2.status == 200) { 
        var replyMsg = xmlhttp2.responseText; 
        alert(replyMsg); 
        if (replyMsg == "CHNGSUCC") { 
         alert("Password changed"); 
         sucess = true; 
        } else if (replyMsg == "CHNGFAIL") { 
         alert("Error: Password is shorter than 8 characters."); 
         sucess = false; 
        } else if (replyMsg == "CHNGERROR") { 
         alert("Internal error, please contact server admin."); 
         sucess = true; 
        } 
       } 
      } 
      xmlhttp2.open("POST","changepw.php",true); 
      xmlhttp2.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
      xmlhttp2.send("uid="+document.getElementById("amsnr").value+"&pwd="+newPassword); 

     } else { 
      alert("Password unchanged"); 
      success = true; 
     } 
    } 

} 
+0

'while' 루프를 사용하는 목적은 무엇인가? – guest271314

+0

즉, 사용자가 잘못된 것을 입력하면 (예 : 8 자 미만의 암호) 사용자가 다른 암호를 입력하라는 메시지가 다시 표시됩니다. –

+0

동기화 코드 (경고)와 비동기 코드 (아약스)를 혼합합니다. 내부 비동기 코드에서 약속을 반환하거나 값이있을 때 호출 할 콜백을 인수로 보낼 수 있습니다. 물론 코드를 리팩터링해야 할 것입니다. – progysm

답변

0

예 :

/** 
* Call Example 
* changePw_prompt(function(changed) { console.log("password changed? ", changed); }) 
*/ 
/** 
* @param function continueCallback : will run with the argument false/true 
*/ 
function changePw_prompt(continueCallback) { 
    var newPassword = prompt("Please enter a new password", ""); 
    if (newPassword == null) { 
      alert("Password unchanged"); 
      continueCallback(false); 
    } else { 
      var xmlhttp2 = new XMLHttpRequest(); 
      xmlhttp2.onreadystatechange = function() { 
       if (xmlhttp2.readyState == 4 && xmlhttp2.status == 200) { 
        var replyMsg = xmlhttp2.responseText; 
        alert(replyMsg); 
        if (replyMsg == "CHNGSUCC") { 
         alert("Password changed"); 
         continueCallback(true); 
        } else if (replyMsg == "CHNGFAIL") { 
         alert("Error: Password is shorter than 8 characters."); 

         // repeat the process 
         changePw_prompt(continueCallback); 
        } else if (replyMsg == "CHNGERROR") { 
         alert("Internal error, please contact server admin."); 
         continueCallback(false); 
        } 
       } 
      } 
      xmlhttp2.open("POST","changepw.php",true); 
      xmlhttp2.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
      var uid = encodeURIComponent(document.getElementById("amsnr").value); 
      var pwd = encodeURIComponent(newPassword); 
      xmlhttp2.send("uid="+uid+"&pwd="+pwd); 
    } 
}