前后端数据交互处理基于原生JS模板引擎开发
json数据错误处理,把json文件数据复制到----> https://www.bejson.com/ 在线解析json
这样能直观的了解到是否是json数据写错,在控制台打断点,那里错误打那里断点,观察是否有错误.
<!DOCTYPE html>
<html>
<head>
<title>前后端数据交互处理原生JS模板引擎开发</title>
<meta charset ='utf-8'> <script type="text/template" id="tpl"><!--用于存储模板-->
<div class='product'>
<div class='imageAll'>
<img src="{#img#}">
<div class='layer'>
<p>找同款</p>
<p>找相似</p>
</div>
</div>
<div class='content'>
<p class='price'>
<span class='price-text'>¥{#price#}</span>
<span class='salas'>{#salas#}人付款</span>
</p>
<p class='title'>{#title#}</p>
<p class='store'>
<span class='store-text'>{#stroe#}</span>
<span class='adress'>{#adress#}</span>
</p>
</div>
</div>
</script> <style>
body{
margin:0;
padding:0;
} #app{
width:1088px;
margin:0 auto;
font-family:'微软雅黑';
}
.product{
float:left;
width:250px;
height:360px;
margin:0 10px;
}
.product:hover{
border:1px solid #F55B24;
}
.imageAll{
position:relative;
width:250px;
height:250px;
}
.imageAll img{
width:250px;
height:220px;
font-size:0;
cursor:pointer;
}
.imageAll .layer{
position:absolute;
left:0;
bottom:0;
width:250px;
height:30px;
}
.imageAll .layer p{
display:block;
float:left;
width:124px;
height:30px;
font-size:12px;
color:#fff;
text-align:center;
line-height:30px;
background-color:#ff6700;
margin:0;
overflow:hidden;
}
.layer p:nth-child(1){
border-right:1px solid #FCA772;
}
.layer p:nth-child(2){
border-left:1px solid #FCA772;
}
.content{
width:250px;
height:110px;
}
.content .price{
width:100%;
height:40px;
line-height:40px;
margin:0;
}
.price .price-text{
font-size:16px;
font-weight:bold;
color:#ff6700;
}
.price .salas{
float:right;
font-size:14px;
color:#A9A3A3;
}
.title{
margin:0;
color:#666;
font-size:12px;
}
.store{
margin:0;
}
.store .store-text{
font-size:12px;
color:#666;
}
.store .adress{
float:right;
font-size:12px;
color:#666;
}
</style>
</head>
<body>
<div id='app'></div> <script>
//命名空间
var Util = {
getId : function(id){
return document.getElementById(id);
},
ajax : function(url,callback){
//创建xhr对象
var xhr = new XMLHttpRequest();
//订阅事件
xhr.onreadystatechange = function(){
if(xhr.readyState === 4){
if(xhr.status === 200){
//返回数据
// var data = JSON.stringify(xhr.responseText);
var data = JSON.parse(xhr.responseText);
//处理数据
callback && callback(data);
}
}
}
//open方法
xhr.open('get',url,true);
//send方法
xhr.send(null);
}
}
/*
获取模板字符串
*/
var html = '';
var tpl = Util.getId('tpl').innerHTML;
//用数据格式化模板
function formString(str,data){
//字符串替换方法(正则匹配模板{#img#} (\w+)任意多个字符)
return str.replace(/\{#(\w+)#\}/g,function(match,$1){
// console.log(this);
// console.log(match,$1);
return data[$1];
});
};
// formString(tpl);
Util.ajax("data/list.json",function(data){
var data = data.list;
for(var i =0; i< data.length; i++){
html += formString(tpl,data[i]);
}
Util.getId('app').innerHTML = html;
// console.log(formString(tpl,data));
})
//测试
/* var html = '';
Util.ajax("data/list.json",function(data){
var list = data.list;
for(var i=0;i<list.length;i++){
html += '<div>'+list[i].adress+'</div>'
}
app.innerHTML = html;
});*/ </script>
</body>
</html>
效果:

前后端数据交互处理基于原生JS模板引擎开发的更多相关文章
- 两种方法实现asp.net方案的前后端数据交互(aspx文件、html+ashx+ajax)
一个HTML页面只能显示HTML代码信息,不能与数据库进行数据的交互.asp.net方案提供了网页与数据库交互的方法,这里举出两种:①aspx文件 ②ashx文件+ajax技术 一.创建数据库 这里以 ...
- 对GraphQL-BFF:微服务背景下的前后端数据交互方案的研究-------引用
随着多终端.多平台.多业务形态.多技术选型等各方面的发展,前后端的数据交互,日益复杂. 同一份数据,可能以多种不同的形态和结构,在多种场景下被消费. 在理想情况下,这些复杂性可以全部由后端承担.前端只 ...
- vue-resource的使用,前后端数据交互
vue-resource的使用,前后端数据交互 1:导入vue与vue-resource的js js下载: https://pan.baidu.com/s/1fs5QaNwcl2AMEyp_kUg ...
- web前后端数据交互
前后端数据交互是每一名web程序员必须熟悉的过程,前后端的数据交互重点在于前端是如何获取后端返回的数据,毕竟后端一般情况下只需要将数据封装到一个jsonMap,然后return就完了.下面通过一个li ...
- 前后端数据交互利器--Protobuf
Protobuf 介绍 Protocol Buffers(又名 protobuf)是 Google 的语言中立.平台中立.可扩展的结构化数据序列化机制. https://github.com/prot ...
- 前后端数据交互(八)——请求方法 GET 和 POST 区别
WEB 开发同学一看 get 和 post 请求方法的区别,第一感觉都是 So easy! 学习ajax.fetch.axios时,发送网络请求携带参数时,都需要分别处理get和post的参数.所以我 ...
- 前后端数据交互(二)——原生 ajax 请求详解
一.ajax介绍 ajax 是前后端交互的重要手段或桥梁.它不是一个技术,是一组技术的组合. ajax :a:异步:j:js:a:和:x:服务端的数据. ajax的组成: 异步的 js 事件 其他 j ...
- SpringMVC前后端数据交互总结
控制器 作为控制器,大体的作用是作为V端的数据接收并且交给M层去处理,然后负责管理V的跳转.SpringMVC的作用不外乎就是如此,主要分为:接收表单或者请求的值,定义过滤器,跳转页面:其实就是ser ...
- Spring MVC前后端数据交互总结
控制器 作为控制器,大体的作用是作为V端的数据接收并且交给M层去处理,然后负责管理V的跳转.SpringMVC的作用不外乎就是如此,主要分为:接收表单或者请求的值,定义过滤器,跳转页面:其实就是ser ...
随机推荐
- webpack 大概
entry output loader: rules: [ {test:匹配文件格式, use: 使用的loader}, {test:匹配文件格式, use: 使用的loader}, ...//l ...
- 在Python工作环境中安装包命令后加上国内源速度*15
example: pip install -r requests.txt -r https://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pyp ...
- GoGland 快捷键说明
关于Gogland一些常用快捷键的说明,我在网上没有搜索到,于是乎,我找到了官网中的视频介绍,然后将其中的一部分摘录了出来,希望能帮住大家... Gogland——使用说明前面是苹果|后面是linux ...
- php导出excel问题之解决
phpExcel采用的版本:1.8.1.0 git地址:https://github.com/PHPOffice/PHPExcel.git 在windows7+nginx的环境中,选择Excel5 ...
- Centos中MySQL数据的备份和恢复
1.MySQL数据备份 MySQL在Centos备份中用到了mysqldump这个文件,首先得把它找出来: [root@instance-3snz20bz ~]# whereis mysqldump ...
- 2018-软工机试-D-定西
单点时限: 1.0 sec 内存限制: 256 MB 这么多年你一个人一直在走 方向和天气的节奏会让你忧愁 你说你遇见了一大堆奇怪的人 他们看上去好像都比你开心 ——李志<定西> 这首歌的 ...
- OFBiz项目简介
记得最早使用OFBiz是十年前在公司的一个EA游戏项目中,用来实现玩家在游戏中购买各种游戏装备.当由于自己刚出校门不久,经验也少,对软件产品架构.思想.目的了解不透彻,不明白OFBiz设计上的优点,本 ...
- React native中DrawerNavigator,StackNavigator,TabNavigator导航栏使用
import React from 'react'; import { View, Text,Button } from 'react-native'; import { DrawerNavigato ...
- Linux 环境下安装Mysql的步骤
一,以linux cent 6.9 安装mysql 5.6.39为例#下载安装包wget --no-check-certificate https://dev.mysql.com/get/Downlo ...
- sqlite当天时间的23:59:59
select strftime('%Y-%m-%d %H:%M:%S','now','+1 day','localtime','start of day','-1 seconds')