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':}

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":"","cat_id":"","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":"","goods_number":"","price":"65.00","goods_thumb":"images\/201604\/thumb_img\/30_thumb_G_1461895713329.jpg","is_real":"","is_on_sale":"","is_alone_sale":"","is_shipping":"","add_time":"","sort_order":"","is_delete":"","is_best":"","is_new":"","is_hot":"","is_promote":"","last_update":"","is_check":null,"barcode":""},{"goods_id":"","cat_id":"","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":"","goods_number":"","price":"84.00","goods_thumb":"images\/201604\/thumb_img\/33_thumb_G_1461895784860.jpg","is_real":"","is_on_sale":"","is_alone_sale":"","is_shipping":"","add_time":"","sort_order":"","is_delete":"","is_best":"","is_new":"","is_hot":"","is_promote":"","last_update":"","is_check":null,"barcode":""},{"goods_id":"","cat_id":"","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":"","goods_number":"","price":"84.00","goods_thumb":"images\/201604\/thumb_img\/34_thumb_G_1461895812866.jpg","is_real":"","is_on_sale":"","is_alone_sale":"","is_shipping":"","add_time":"","sort_order":"","is_delete":"","is_best":"","is_new":"","is_hot":"","is_promote":"","last_update":"","is_check":null,"barcode":""},{"goods_id":"","cat_id":"","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":"","goods_number":"","price":"85.00","goods_thumb":"images\/201603\/thumb_img\/35_thumb_G_1458064454764.jpg","is_real":"","is_on_sale":"","is_alone_sale":"","is_shipping":"","add_time":"","sort_order":"","is_delete":"","is_best":"","is_new":"","is_hot":"","is_promote":"","last_update":"","is_check":null,"barcode":""}]

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=;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);
}
})
上一篇:测试markdown发布


下一篇:如何构建你自己的Java库【翻译】