zoukankan      html  css  js  c++  java
  • 基于HTML模板和JSON数据的JavaScript交互

    为了将后台的数据处理放到前台,我们可能出现以下的代码

    html

    
    
    
    String.prototype.temp = function(obj) {
    return this.replace(/$w+$/gi, function(matchs) {
    var returns = obj[matchs.replace(/$/g, "")];
    return (returns + "") == "undefined"? "": returns;
    });
    };
    

    json

     
    {
        "ecd":0,
        "msg" :"成功",
        "result" : [{
        "id": "32",
        "order_num": "test-001",
        "title": "test",
        "thumb": "http://40DA1265-40F6-D622-8BA5-04BA0AF72573.jpg",
        "item_id": "21",
        "price": "0.11",
        "cus_name": "test",
        "cus_tel": "10086",
        "cus_address": "北京 北京海淀区",
        "flag": "5",
        "create_time": "20160329115544",
        "update_time": "20160330120001",
        "flag_name": "订单已取消"
    }],
        "locate":""
    } 
    

    js

    $.progress_show('正在努力加载中');
    $.ajax({
        url: site_url + 'api/order/getAll/' + status,
        type: 'get',
        dataType: 'json',
        error: doAjax.error,
        success: function (response) {
            $.progress_hide();
            if (response.ecd == '0') {
                var htmlList = '', htmlTemp = $("textarea.js-order-tmp").val();
                if (typeof response.result === 'undefined') {
                    htmlList = $("textarea.js-no-order-tmp").val();
                } else {
                    $.each(response.result, function (i, el) {
                        htmlList += htmlTemp.temp(el);
                    });
                }
                $('.js-status-' + status).empty().append(htmlList);
                returntrue;
            } else {
                return $.alert(response.msg);
            }
        },
    });
    
  • 相关阅读:
    webstorm 对 vue的设置
    chrome的adblock插件报DevTools failed to parse SourceMap
    textContent和innerText属性的区别
    心得体悟帖---200417(做人做事一定要有策略,人和人之间的交互都是博弈,对每个人本能的防御机制)
    心得体悟帖---200417(最坏的结局)
    心得体悟帖---200417(好好整理下外形,对提升信心特别有帮助)
    树莓派系统的常用的命令
    树莓派
    fastjson中转字符串时格式化、显示null值等
    JSONPath
  • 原文地址:https://www.cnblogs.com/heyinwangchuan/p/6337561.html
Copyright © 2011-2022 走看看