[ASP.NET][JQuery]如何在前端呼叫 Web Services 和接收回傳的 JSON 結果
JQuery的應用越來越普遍,當然JQuery也提供我們以AJAX的方式呼叫Web Services來取得回傳值 請參考下列程式碼
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript" language="javascript">
google.load("jquery", "1.4");
function testJson() {
$.ajax({
type: "POST",
url: "GreenPower.asmx/InfoTableJSON",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
var data = eval("(" + msg.d + ")");
var t = "<table border='0' width='400px'><tr>" +
"<td><strong> IP 位置</strong></td> <td> " +
"<strong>地 點</strong></td> <td> " +
"<strong>燈泡時數</strong></td> <td> " +
"<strong>溫 度</strong></td> ";
jQuery.each(data, function(rec) {
t = t + " <tr> <td> " + this.HostIP + "</td> <td> " + this.HostBuilding + this.HostFloor + this.HostRoom + "</td> <td> " +
this.LampHour + "小時</td><td> " + this.Temperature + "度</td><tr> ";
});
t = t + " </table> ";
$("#jsonDiv").html(t);
},
error: function(msg) {
alert("err");
}
});
};
</script>
如果是用ASP.NET 2.0撰寫, 請使用Msg 接收回傳值
如果是用ASP.NET 3.5撰寫, 請使用Msg.d 接收回傳值
差別主要在於.d這個方式