2012-03-21 9 views
0

MS SQL 2008データベースに接続して情報を取得し、結果セットをクラスに渡してjsonarrayに変換し、その情報を返すjavaサーブレットがあります。私はhttp://localhost:8080/KReport/GetInfo?q=va-sql2008を使用して、私のブラウザからクラスを参照するときここJSONArray - javascript(jsp)を使用して表示する方法

は返さ情報のサンプルです:

[{"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としてチェックアウトします。

サーブレットは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> 

しかし、それは動作しませんでした - ちょうど空白の画面を。

助けていただけたら幸いです。

答えて

0

console.logは、JavaScriptコンソールにデータを出力します。ページに出力する場合は、DOM manipulationを実行する必要があります。

0

console.logブラウザのデベロッパーツールにデータを出力します。ページ上に表示するには、successコールバック:

$('body').append("<pre>" + JSON.stringify(data, undefined, 2) + "</pre>"); 
関連する問題