我有一個Java servlet連接到MS SQL 2008數據庫並檢索信息,將結果集傳遞給類將其轉換爲jsonarray,然後返回該信息。JSONArray - 如何顯示使用JavaScript(jsp)
這裏是返回信息的樣本時,我使用http://localhost:8080/KReport/GetInfo?q=va-sql2008瀏覽到從我的瀏覽器類:
[{"machineinfo":{"DefaultGateway":"172.24.1.2","groupName":"servers.headoffice.vasa","ChassisManufacturer":"No Enclosure","DhcpEnabled":2,"LoginName":"","MotherboardProductCode":"440BX Desktop Reference Platform","MaxMemorySlots":"15","Machine_GroupID":"va-sql2008dev.servers.headoffice.vasa","timezoneOffset":-120,"ChassisSerialNumber":"None","MajorVersion":6,"MotherboardVersion":"None","MaxMemorySize":"256 GB","Manufacturer":"VMware, Inc.","MacAddr":"00-50-56-9F-00-11","ProductName":"VMware Virtual Platform","agentGuid":871664384736872,"OsInfo":"R2 Server Standard x64 Edition Build 7600","PrimaryWinsServer":"172.24.1.9","SysSerialNumber":"VMware-42 1f 09 c3 bd 8c ad 60-22 e4 50 1c 35 52 76 fe","OsType":"2008","MotherboardManufacturer":"Intel Corporation","ipv6Address":"[fe80::edf0:d1bd:6ef8:a17d%11]","MachineVersion":"None","IpAddress":"172.24.1.184","DnsServer1":"172.24.1.22","DnsServer2":"172.24.1.29","ComputerName":"va-sql2008dev","ChassisAssetTag":"No Asset Tag","ChassisType":"Other","MotherboardSerialNumber":"None","SubnetMask":"255.255.255.0","MinorVersion":1,"agentInstGuid":"KSY99906876289809604","WinsEnabled":1,"ConnectionGatewayIp":"172.24.1.184","machName":"va-sql2008dev","ChassisVersion":"N\/A","BusSpeed":"0 MHz"}},{"machineinfo":{"DefaultGateway":"172.24.1.2","groupName":"servers.headoffice.vasa","ChassisManufacturer":"No Enclosure","DhcpEnabled":2,"LoginName":"","MotherboardProductCode":"440BX Desktop Reference Platform","MaxMemorySlots":"15","Machine_GroupID":"va-sql2008dev.servers.headoffice.vasa","timezoneOffset":-120,"ChassisSerialNumber":"None","MajorVersion":6,"MotherboardVersion":"None","MaxMemorySize":"256 GB","Manufacturer":"VMware, Inc.","MacAddr":"00-50-56-9F-00-11","ProductName":"VMware Virtual Platform","agentGuid":871664384736872,"OsInfo":"R2 Server Standard x64 Edition Build 7600","PrimaryWinsServer":"172.24.1.9","SysSerialNumber":"VMware-42 1f 09 c3 bd 8c ad 60-22 e4 50 1c 35 52 76 fe","OsType":"2008","MotherboardManufacturer":"Intel Corporation","ipv6Address":"[fe80::edf0:d1bd:6ef8:a17d%11]","MachineVersion":"None","IpAddress":"172.24.1.184","DnsServer1":"172.24.1.22","DnsServer2":"172.24.1.29","ComputerName":"va-sql2008dev","ChassisAssetTag":"No Asset Tag","ChassisType":"Other","MotherboardSerialNumber":"None","SubnetMask":"255.255.255.0","MinorVersion":1,"agentInstGuid":"KSY99906876289809604","WinsEnabled":1,"ConnectionGatewayIp":"172.24.1.184","machName":"va-sql2008dev","ChassisVersion":"N\/A","BusSpeed":"0 MHz"}}]
裏面還有2臺所列機器(如果您搜索machineinfo在那裏你會發現2項)。我已經驗證了JSONLint返回的信息,並將其作爲有效的json檢出。
我的servlet通過response.getWriter().write(njson.toString());
返回信息,但我似乎無法弄清楚如何在另一頁上使用javascript顯示此信息。
我最後一次嘗試使用:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<script type="text/javascript">
$.ajax({
url:'http://localhost:8080/KReport/GetInfo?q=va-sql2008',
dataType:'json',
type:'GET',
success:function(data){
console.log(data);
},
error:function(jxhr){
console.log(jxhr.responseText);
}
});
</script>
</html>
,但沒有奏效 - 只是一個空白屏幕。
任何幫助將不勝感激?