jquery,php之间的ajax关系以及json
1.最简介写法
function AjaxDepotGoods(id){ $.ajax({ url:"{:U('stock/depot_goods')}", success:function(html){ $('#depot_goods').html(html) } }); }
2.进阶写法
$.ajax({ url:"{:U('pur/SerAjaxGoods')}", type: 'GET', data:{'keywords':$keywords}, success:function (html){alert(html); }, error:function(){ alert('查询错误') } })
3.json返回数据
php 输出代码
{'name':'twobin','age':24}
js代码
$.ajax({ url:"{:U('pur/SerAjaxGoods')}", type: 'GET', data:{'keywords':$keywords}, dataType: "json", success:function ({
alert(html.name);
})
4.【高级json】php,ajax,json 返回的json是多维数组
php输出的代码
[{"goods_id":"30","cat_id":"266","goods_sn":"AHC-625","goods_name":"\u97e9\u56fd AHC B5 \u9ad8\u6548\u6c34\u5408\u8212\u7f13\u4fdd\u6e7f\u6d17\u9762\u5976\uff08\u5347\u7ea7\u7248\uff09","brand_id":"10","goods_number":"5004","price":"65.00","goods_thumb":"images\/201604\/thumb_img\/30_thumb_G_1461895713329.jpg","is_real":"1","is_on_sale":"1","is_alone_sale":"1","is_shipping":"1","add_time":"1456538997","sort_order":"100","is_delete":"0","is_best":"1","is_new":"0","is_hot":"0","is_promote":"0","last_update":"1464541057","is_check":null,"barcode":"8809471951116"},{"goods_id":"33","cat_id":"203","goods_sn":"AHC-605","goods_name":"\u97e9\u56fd AHC \u9ad8\u6d53\u5ea6\u7ef4C\u9ad8\u6548\u51fb\u6591\u51dd\u767d\u9762\u819c\u5347\u7ea7\u7248","brand_id":"10","goods_number":"2005","price":"84.00","goods_thumb":"images\/201604\/thumb_img\/33_thumb_G_1461895784860.jpg","is_real":"1","is_on_sale":"1","is_alone_sale":"1","is_shipping":"1","add_time":"1456538997","sort_order":"100","is_delete":"0","is_best":"0","is_new":"0","is_hot":"0","is_promote":"0","last_update":"1464115989","is_check":null,"barcode":"8809091729942"},{"goods_id":"34","cat_id":"203","goods_sn":"AHC-606","goods_name":"\u97e9\u56fd AHC \u9ad8\u6d53\u5ea6PCG\u80f6\u539f\u86cb\u767d\u7d27\u80a4\u9762\u819c\u5347\u7ea7\u7248","brand_id":"10","goods_number":"2005","price":"84.00","goods_thumb":"images\/201604\/thumb_img\/34_thumb_G_1461895812866.jpg","is_real":"1","is_on_sale":"1","is_alone_sale":"1","is_shipping":"1","add_time":"1456538997","sort_order":"100","is_delete":"0","is_best":"0","is_new":"0","is_hot":"0","is_promote":"0","last_update":"1464115989","is_check":null,"barcode":"8809091729966"},{"goods_id":"35","cat_id":"203","goods_sn":"AHC-607","goods_name":"\u97e9\u56fd AHC \u9ad8\u6d53\u5ea6B5\u6c34\u5408\u900f\u660e\u8d28\u9178\u9762\u819c\u5347\u7ea7\u7248","brand_id":"10","goods_number":"2005","price":"85.00","goods_thumb":"images\/201603\/thumb_img\/35_thumb_G_1458064454764.jpg","is_real":"1","is_on_sale":"1","is_alone_sale":"1","is_shipping":"1","add_time":"1456538997","sort_order":"100","is_delete":"0","is_best":"1","is_new":"0","is_hot":"0","is_promote":"0","last_update":"1464115989","is_check":null,"barcode":"8809471950546"}]
js代码
json个数: (html.length)
$.ajax({ url:"{:U('pur/SerAjaxGoods')}", type: 'GET', data:{'keywords':$keywords}, dataType: "json", success:function (html){ $str = '<table width="100%" border="0" cellspacing="0" cellpadding="0" class="tab">'; for(i=0;i<html.length;i++){ $str += '<tr>'; $str +='<td>'+html[i]['goods_id']+'</td>'; $str +='<td>'+html[i]['goods_name']+'</td>'; $str +='<td>'+html[i]['goods_sn']+'</td>'; $str +='<td>'+html[i]['goods_barcode']+'</td>'; $str +='</tr>'; } $str +='</table>' $('.sel_goods').html($str); //alert(html[0]['goods_id']); }, error:function(){ $str = '<table width="100%" border="0" cellspacing="0" cellpadding="0" class="tab">'; $str += '<tr colspan="4">'; $str +='<td align="center"><strong>查询出错,请重新</strong></td>'; $str += '</tr>'; $str +='</table>' $('.sel_goods').html($str); } })