『壹』 鎬庝箞浣跨敤Ajax瀹炵幇寰鐜
杩欐$粰澶у跺甫鏉ユ庝箞浣跨敤Ajax瀹炵幇寰鐜锛屼娇鐢ˋjax瀹炵幇寰鐜鐨勬敞鎰忎簨椤规湁鍝浜涳紝涓嬮潰灏辨槸瀹炴垬妗堜緥锛屼竴璧锋潵鐪嬩竴涓嬨
Ajax 绠浠
Ajax 鐢 HTML銆丣avaScript? 鎶鏈銆丏HTML 鍜 DOM 缁勬垚锛岃繖涓鏉板嚭鐨勬柟娉曞彲浠ュ皢绗ㄦ嫏鐨 Web 鐣岄潰杞鍖栨垚浜や簰鎬х殑 Ajax 搴旂敤绋嬪簭銆傛湰鏂囩殑浣滆呮槸涓浣 Ajax 涓撳讹紝浠栨紨绀轰簡杩欎簺鎶鏈濡備綍鍗忓悓宸ヤ綔 鈥斺 浠庢讳綋姒傝堪鍒扮粏鑺傜殑璁ㄨ 鈥斺 浣块珮鏁堢殑 Web 寮鍙戞垚涓虹幇瀹炪備粬杩樻彮寮浜 Ajax 鏍稿績姒傚康鐨勭炵橀潰绾憋紝鍖呮嫭 XMLHttpRequest 瀵硅薄銆
浜斿勾鍓嶏紝濡傛灉涓嶇煡閬 XML锛屾偍灏辨槸涓鍙鏃犱汉閲嶈嗙殑涓戝皬楦銆傚崄鍏涓鏈堝墠锛孯uby 鎴愪簡鍏虫敞鐨勪腑蹇冿紝涓嶇煡閬 Ruby 鐨勭▼搴忓憳鍙鑳藉潗鍐锋澘鍑充簡銆備粖澶╋紝濡傛灉鎯宠窡涓婃渶鏂扮殑鎶鏈鏃跺皻锛岄偅鎮ㄧ殑鐩鏍囧氨鏄 Ajax銆
浣嗘槸锛孉jax 涓嶄粎浠 鏄涓绉嶆椂灏氾紝瀹冩槸涓绉嶆瀯寤虹綉绔欑殑寮哄ぇ鏂规硶锛岃屼笖涓嶅儚瀛︿範涓绉嶅叏鏂扮殑璇瑷閭f牱鍥伴毦銆
涓銆佷笟鍔¢渶姹
鍦ㄥ紑鍙戜腑锛屽綋涓涓鍒楄〃椤甸潰鍔犺浇瀹屾垚鍚庯紝鎴戦渶瑕佹牴鎹鍒楄〃姣忎竴椤圭殑id鍘绘湇鍔″櫒绔鑾峰彇瀵瑰簲鐨勬暟鎹鐒跺悗鍐嶆妸鑾峰彇鐨勬暟鎹璧嬬粰褰撳墠id瀵瑰簲鐨勬爣绛俱
渚嬪傚備笅琛ㄦ牸锛
鎴戞湁涓绯诲垪鐨勫晢鍝佺紪鍙凤紝鎴戦渶瑕佹牴鎹鍟嗗搧缂栧彿閫氳繃ajax鍒版湇鍔″櫒鑾峰彇鍟嗗搧瀵瑰簲鐨勫悕绉帮紝鐒跺悗鍐嶇敤js鏇存柊鐣岄潰(瀹為檯涓鐨勪笟鍔″綋鐒朵笉鏄鑾峰彇鍟嗗搧鍚嶇О杩欎箞绠鍗)
浜屻佸疄鐜版柟妗
2.1 閿欒鏂规
涓鑸鎯呭喌涓嬶紝鎴戜滑浼氱洿鎺ユ兂鍒帮紝鐩存帴鍐欎釜for寰鐜锛屽湪寰鐜鍐呭彂璧穉jax璇锋眰鑾峰彇鏁版嵁锛屽啀鎶婅幏鍙栫殑鏁版嵁鏇存柊鍒板瑰簲鐨刬d瀵瑰簲鐨勬爣绛句笂锛
濡備笅锛
鎴戜滑浠ユ暟缁勬ā鎷熶竴浜涘垪id锛
var array = [1, 3, 2, 5, 3];寰鐜鐨刟jax璇锋眰鏂规硶锛
function foreach_ajax() {
for (var i = 0; i < array.length; i++) {
$.get("/home/loop_ajax", { value: array[i] }, function (data) {
console.log(array[i]+","+data);
});
}
}璋冪敤锛
$(function () {
foreach_ajax();
});娴嬭瘯缁撴灉濡備笅锛
鎴戜滑鍙浠ョ湅鍒帮紝鍦ㄥ惊鐜鐨勫唴閮ㄦ垜浠鏍规湰鍙栦笉鍒癮rray[i]鐨勫笺
瀵艰嚧杩欑嶇粨鏋滅殑鍘熷洜鏄锛歛jax鏄寮傛ユ墽琛岀殑锛屽湪寰鐜缁撴潫鐨勬椂鍊欑涓娆ajax杩樻病鏈夊皢鏈嶅姟鍣ㄦ暟鎹杩斿洖锛岃屽惊鐜缁撴潫鐨勬椂鍊檉or涓鐨勫彉閲廼宸茬粡閲婃斁浜嗭紝鎵浠array[i]=undefined
2.2 姝g‘鏂规
姝g‘鐨勬柟寮忔槸浠ラ掑綊鐨勬柟寮忓惊鐜痑jax銆
濡備笅锛
鎴戜滑浠ユ暟缁勬ā鎷熶竴浜涘垪id锛
var array = [1, 3, 2, 5, 3];閫掑綊鐨刟jax璇锋眰鏂规硶锛
function Loop_ajax(index, array) {
if (index < array.length) {
var value = array[index];
$.get("/home/loop_ajax", { value: value }, function (data) {
console.log(array[index] + "," + data);
if (index < array.length) {
Loop_ajax(index + 1, array);
}
});
}
}璋冪敤锛
$(function () {
Loop_ajax(0, array);
});娴嬭瘯缁撴灉濡備笅锛
鐩镐俊鐪嬩簡鏈鏂囨堜緥浣犲凡缁忔帉鎻′簡鏂规硶锛屾洿澶氱簿褰╄峰叧娉℅xl缃戝叾瀹冪浉鍏虫枃绔狅紒
鎺ㄨ崘闃呰伙細
ajax鍙浠ュ勭悊鏈嶅姟鍣ㄨ繑鍥炲摢浜涙暟鎹绫诲瀷锛
瀹岀編澶勭悊json鏁版嵁鏃犳硶鎵ц宻uccess