0

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

我在 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>

我错过了什么,因为它没有返回任何东西:(

4

1 回答 1

0

我相信您对“accountholderid”的 id 值是在寻找“Id”而不是“playerid”。

请问您为什么在价值上调用“评估”?你需要 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 声明、===、到处都是卷曲、单变量模式等)。

于 2013-04-24T20:52:41.280 回答