2013-04-24 41 views
0

我正在敲定一個大學項目,我被卡住了。Javascript查詢銀行API

我在netbeans中創建了一個API,它工作正常。

回覆例如

<?xml version="1.0" encoding="UTF-8"?> 
    <accountholder> 
     <accountnumber>45672</accountnumber> 
     <address>234 THE BANK, DUBLIN 1</address> 
     <balance>763.32</balance> 
     <email>[email protected]</email> 
     <firstname>JOHANN</firstname> 
     <id>1</id> 
     <lastname>SMITH</lastname> 
     <pinnumber>1234</pinnumber> 
    </accountholder> 

現在我試圖創建一個JavaScript來通過Id搜索時返回數據。

<script language="javascript" type="text/javascript"> 
var request = null; 

function createRequest() { 
try { 
request = new XMLHttpRequest(); 
} catch (trymicrosoft) { 
try { 
request = new ActiveXObject("MsXML2.XMLHTTP"); 
} catch (othermicrosoft) { 
try { 
request = new ActiveXObject("Microsoft.XMLHTTP"); 
} catch (failed) { 
request = null; 
} 
} 
} 

if (request == null) 
alert("Error creating request object!"); 
} 
function getMessage() 
{ 
createRequest(); 
var accountholderid = document.getElementById("Id").value; 
id=eval(accountholderid); 
var url = "http://localhost:8080/BankProjectApi/webresources/bankprojectapi.accountholder/"+id; 
request.onreadystatechange = handleResponse; 
request.open("GET", url, true); 
request.send(null); 
} 
function handleResponse() { 
if (request.readyState==4 && request.status==200) 
{ 
var xmlDocument=request.responseXML; 
var firstname = xmlDocument.getElementsByTagName("firstname"); 
var lastname = xmlDocument.getElementsByTagName("lastname"); 
var accountnumber = xmlDocument.getElementsByTagName("accountnumber"); 

for(var i=0; i<firstname.length; i++) { 
var firstname = firstname[i].childNodes[0].nodeValue; 
var lastname = lastname[i].childNodes[0].nodeValue; 
var accountnumber= accountnumber[i].childNodes[0].nodeValue; 
document.getElementById('lastname').value=firstname; 
document.getElementById('firstname').value=lastname; 
document.getElementById('accountnumber').value=accountnumber; 
} 
} 

} 
</script> 

在身體我有一個按鈕輸入文本框與上點擊:

<td>Enter Account holder ID : </td> 
<td><input type="text" id="playerid" size="10"/> 

<input type="button" value="Get Details" onclick="getMessage()"/> 
</tr> 
<tr> 
<td>Account holder Last Name : </td> 
<td> <input type="text" id="lastname" size="10"/> </td> 
</tr> 

<tr> 
<td>Account holder First Name : </td> 
<td> <input type="text" id="firstname" size="10"/> </td> 
</tr> 

<tr> 
<td>Account number : </td> 
<td> <input type="text" id="accountnumber" size="10"/> </td> 
</tr> 

我缺少什麼,因爲它不返回任何東西:(

回答

1

我相信你的身份證'accountholderid'的值是'Id'而不是'playerid'。

請問你爲什麼要調用'eval'的值?你需要parseInt嗎?

(function() { 

var request = null; 

function createRequest() { 
    try { 
     request = new XMLHttpRequest(); 
    } catch (trymicrosoft) { 
     try { 
      request = new ActiveXObject('MsXML2.XMLHTTP'); 
     } catch (othermicrosoft) { 
      try { 
       request = new ActiveXObject('Microsoft.XMLHTTP'); 
      } catch (failed) { 
       request = null; 
      } 
     } 
    } 

    if (request === null) { 
     alert('Error creating request object!'); 
    } 
} 

function getMessage() { 
    createRequest(); 

    var accountholderid = document.getElementById('playerid').value, 
     id = eval(accountholderid), 
     url = 'http://localhost:8080/BankProjectApi/webresources/bankprojectapi.accountholder/' + id; 

    request.onreadystatechange = handleResponse; 
    request.open("GET", url, true); 
    request.send(null); 
} 

function handleResponse() { 
    if (request.readyState === 4 && request.status === 200) { 
     var xmlDocument = request.responseXML, 
      firstname = xmlDocument.getElementsByTagName('firstname'), 
      lastname = xmlDocument.getElementsByTagName('lastname'), 
      accountnumber = xmlDocument.getElementsByTagName('accountnumber'); 

     for(var i = 0, max = firstname.length; i < max; i += 1) { 
      var firstname = firstname[i].childNodes[0].nodeValue, 
       lastname = lastname[i].childNodes[0].nodeValue, 
       accountnumber = accountnumber[i].childNodes[0].nodeValue; 

      document.getElementById('lastname').value = firstname; 
      document.getElementById('firstname').value = lastname; 
      document.getElementById('accountnumber').value = accountnumber; 
     } 
    } 

} 

}()); 

此外,我做了一個代碼的快速重構,以幫助我評估問題,堅持更多的社區約定以及避免常見的JS陷阱。 (例如關閉,缺少var聲明,===,捲曲到處,單個可變模式,以及其他一些)。

+0

嗨Wintondeshong 播放器ID來自我使用的Oracle教程。我改變了事情以適應我的銀行業務項目,並使用Id代替。一定是錯過了它的某個地方?但我不認爲我在使用playerId – user2110655 2013-04-24 20:57:31

+0

Wintondeshong,我希望我能給你一個+1,你是一個天才!它的工作原理 – user2110655 2013-04-24 21:03:11

+0

你可能也想仔細看看循環http://cl.ly/image/2U2s3i0w1q1P,因爲它正在覆蓋你的局部變量。但我會把它留給你;) – wintondeshong 2013-04-24 21:19:33