使用get()方法以GET方式從服務(wù)器獲取數(shù)據(jù)
使用get()
方法時(shí),采用GET方式向服務(wù)器請(qǐng)求數(shù)據(jù),并通過(guò)方法中回調(diào)函數(shù)的參數(shù)返回請(qǐng)求的數(shù)據(jù),它的調(diào)用格式如下:
$.get(url,[callback])
參數(shù)url為服務(wù)器請(qǐng)求地址,可選項(xiàng)callback參數(shù)為請(qǐng)求成功后執(zhí)行的回調(diào)函數(shù)。
例如,當(dāng)點(diǎn)擊“加載”按鈕時(shí),調(diào)用get()
方法向服務(wù)器中的一個(gè).php文件以GET方式請(qǐng)求數(shù)據(jù),并將返回的數(shù)據(jù)內(nèi)容顯示在頁(yè)面中,如下圖所示:

在瀏覽器中顯示的效果:

從圖中可以看出,通過(guò)$.get()
方法向服務(wù)器成功請(qǐng)求數(shù)據(jù)后,在回調(diào)函數(shù)中通過(guò)data參數(shù)傳回請(qǐng)求的數(shù)據(jù),并以data.name格式訪問(wèn)數(shù)據(jù)中各項(xiàng)的內(nèi)容。
任務(wù)
我來(lái)試試,親自使用$.get()
方法請(qǐng)求服務(wù)器中的數(shù)據(jù)
在下列代碼的第23行,使用$.get()
方法,請(qǐng)求服務(wù)器中 http://idcbgp.cn/data/info_f.php
文件中的數(shù)據(jù),并將返回的內(nèi)容顯示在頁(yè)面中。

- ?不會(huì)了怎么辦
-
- 在使用
$.get()
方法請(qǐng)求服務(wù)器數(shù)據(jù)時(shí),url參數(shù)為服務(wù)器端的文件路徑,第二個(gè)參數(shù)為回調(diào)函數(shù),在該函數(shù)中,通過(guò)參數(shù)傳回請(qǐng)求獲取的數(shù)據(jù)。
- “$.get”書(shū)寫(xiě)是否正確,它們之間通過(guò).點(diǎn)號(hào)進(jìn)行鏈接。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>使用get()方法以GET方式從服務(wù)器獲取數(shù)據(jù)</title>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js" type="text/javascript"></script>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="divtest">
<div class="title">
<span class="fl">我的個(gè)人資料</span>
<span class="fr">
<input id="btnShow" type="button" value="加載" />
</span>
</div>
<ul></ul>
</div>
<script type="text/javascript">
$(function () {
$("#btnShow").bind("click", function () {
var $this = $(this);
? {
$this.attr("disabled", "true");
$("ul").append("<li>我的名字叫:" + data.name + "</li>");
$("ul").append("<li>男朋友對(duì)我說(shuō):" + data.say + "</li>");
}, "json");
})
});
</script>
</body>
</html>
#divtest
{
width: 282px;
}
#divtest .title
{
padding: 8px;
background-color:blue;
color:#fff;
height: 23px;
line-height: 23px;
font-size: 15px;
font-weight: bold;
}
ul
{
float: left;
width: 280px;
padding: 5px 0px;
margin: 0px;
font-size: 14px;
list-style-type: none;
}
ul li
{
float: left;
width: 280px;
height: 23px;
line-height: 23px;
padding: 3px 8px;
}
.fl
{
float: left;
}
.fr
{
float: right;
}
請(qǐng)驗(yàn)證,完成請(qǐng)求
由于請(qǐng)求次數(shù)過(guò)多,請(qǐng)先驗(yàn)證,完成再次請(qǐng)求