zoukankan      html  css  js  c++  java
  • 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":"u97e9u56fd AHC B5 u9ad8u6548u6c34u5408u8212u7f13u4fddu6e7fu6d17u9762u5976uff08u5347u7ea7u7248uff09","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":"u97e9u56fd AHC u9ad8u6d53u5ea6u7ef4Cu9ad8u6548u51fbu6591u51ddu767du9762u819cu5347u7ea7u7248","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":"u97e9u56fd AHC u9ad8u6d53u5ea6PCGu80f6u539fu86cbu767du7d27u80a4u9762u819cu5347u7ea7u7248","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":"u97e9u56fd AHC u9ad8u6d53u5ea6B5u6c34u5408u900fu660eu8d28u9178u9762u819cu5347u7ea7u7248","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);    
            }    
        })
  • 相关阅读:
    [LeetCode] 85. 最大矩形
    [LeetCode] 84. 柱状图中最大的矩形
    [LeetCode] 83. 删除排序链表中的重复元素
    [LeetCode] 81. 搜索旋转排序数组 II
    [LeetCode] 82. 删除排序链表中的重复元素 II
    [LeetCode] 80. 删除排序数组中的重复项 II
    [LeetCode] 79. 单词搜索
    [LeetCode] 77. 组合
    转:快乐Node码农的十个习惯
    转:zookeeper3.4.5安装笔记
  • 原文地址:https://www.cnblogs.com/wesky/p/5559280.html
Copyright © 2011-2022 走看看