通过Javascript查询银行API

时间:2013-04-24 20:34:44

标签: api netbeans glassfish banking

我正在完成一个大学项目,我被困住了。

我已经在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>JOHANN@SMITH.COM</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 个答案:

答案 0 :(得分:1)

我相信&#39; accountholderid&#39;正在寻找&#39; Id&#39;而不是&#39; playerid&#39;。

我可以问你为什么要打电话给#val;&#39; eval&#39;在价值?你需要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声明,===,到处都是卷曲,单变量模式,以及其他一些)。