1:首先ecshop是如何定義ajax對(duì)象的。
ecshop中的ajax對(duì)象是在js/transport.js文件中定義的。里面是ajax對(duì)象文件。聲明了一個(gè)var Ajax = Transport;對(duì)象和一個(gè)方法Ajax.call = Transport.run;
2:ecshop中ajax可以使用兩種方式傳遞數(shù)據(jù).一種是get方式,一種是post方式.
Ajax.call( 'user.php?act=is_registered', 'username=' + username, registed_callback , 'GET', 'TEXT', true, true );
Ajax.call('user.php?act=return_to_cart', 'order_id=' + orderId, returnToCartResponse, 'POST', 'JSON');
3:ecshop中的 ajax可以是傳遞text數(shù)據(jù),也可以是一個(gè)json對(duì)象。比如以下代碼
goods.quick = quick;
goods.spec = spec_arr;
goods.goods_id = goodsId;
goods.number = number;
goods.parent = (typeof(parentId) == "undefined") ? 0 : parseInt(parentId);
Ajax.call('flow.php?step=add_to_cart', 'goods=' + goods.toJSONString(), addToCartResponse, 'POST', 'JSON');
里面的goods就是對(duì)象.而且是靠json來(lái)傳遞的。返回的結(jié)果result也是對(duì)象.
4:ecshop ajax函數(shù)里面.第三個(gè)參數(shù)就是回掉函數(shù)的名稱(chēng)。比如以上代碼addToCartResponse 這個(gè)函數(shù)就是ajax處理結(jié)果的回調(diào)函數(shù).
5:在ecshop的php代碼中,一般是通過(guò)get或者post方式來(lái)接受函數(shù)。比如以下例子,如果接受的是對(duì)象。還需要用json數(shù)據(jù)格式來(lái)處理.比如以下
include_once('includes/cls_json.php');
$_POST['goods'] = json_str_iconv($_POST['goods']);
處理的返回結(jié)果,也需要是json格式發(fā)送給js
die($json->encode($result));
例如:
<input type="button" name="search" value="{$lang.button_search}" onclick="searchUser();" /> <select name="user_search[]" id="user_search" size="15" style="width:260px" ondblclick="addUser()" multiple="true"> </select>
/** * 按用戶名搜索用戶 */ function searchUser() { var eles = document.forms['theForm'].elements; /* 填充列表 */ var keywords = Utils.trim(eles['keyword'].value); if (keywords != '') { Ajax.call('bonus.php?is_ajax=1&act=search_users', 'keywords=' + keywords, searchUserResponse, "GET", "JSON"); } } function searchUserResponse(result) { var eles = document.forms['theForm'].elements; eles['user_search[]'].length = 0; if (result.error == 0) { for (i = 0; i < result.content.length; i++) { var opt = document.createElement('OPTION'); opt.value = result.content[i].user_id; opt.text = result.content[i].user_name; eles['user_search[]'].options.add(opt); } } }
/*------------------------------------------------------ */ //-- 搜索用戶 /*------------------------------------------------------ */ if ($_REQUEST['act'] == 'search_users') { $keywords = json_str_iconv(trim($_GET['keywords'])); $sql = "SELECT user_id, user_name FROM " . $ecs->table('users') . " WHERE user_name LIKE '%" . mysql_like_quote($keywords) . "%' OR user_id LIKE '%" . mysql_like_quote($keywords) . "%'"; $row = $db->getAll($sql); make_json_result($row); }
來(lái)源:http://blog.csdn.net/21aspnet/article/details/7635575
發(fā)表評(píng)論