前后端数据交互处理基于原生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 ...
随机推荐
- Linux 文件系统(一)---虚拟文件系统VFS----超级块、inode、dentry、file
转自:http://blog.csdn.net/shanshanpt/article/details/38943731 http://elixir.free-electrons.com/linux/v ...
- eclipse代码自动补全。
打开 Eclipse -> Window -> Perferences 找到Java 下的 Editor 下的 Content Assist , 右边出现的选项中,有一个Auto acti ...
- java完整并发知识结构图
一张大的java并发知识结构图,梳理清楚知识的脉络,知识不再零散
- Exploit-Exercises nebule 旅行日志(五)
接着上次的路程继续在ubuntu下对漏洞的探索练习,这次是level04了 先看下level04的问题描述: (level4.c) #include <stdlib.h> #include ...
- CSS&JS小结
回顾:html: 作用:展示 文件标签: <html> <head> <title></title> </head> <body> ...
- vue路由的懒加载
一.懒加载 也叫延迟加载或者按需加载,即在需要的时候进行加载, 二.为什么要使用懒加载 像vue这种单页面应用,如果没有应用懒加载,运用webpack打包后的文件将会异常的大,造成进入首页时,需要 ...
- tcl实现批量压缩文件夹
tcl脚本本身对字符串的处理比较简单,所以想着用这个也实现下: proc main {} { puts "请输入路径:" set strpath "E:\\123&quo ...
- mysql_pconnect 问题
不同于mysql_connect的短连接,mysql_pconnect持久连接的时候,将先尝试寻找一个在同一个主机上用同样的用户名和密码已经打开的(持久)连接,如果找到,则返回此连接标识而不打开新连接 ...
- Python 内编写类的各种技巧和方法
Python 内编写类的各种技巧和方法 简介 有关 Python 内编写类的各种技巧和方法(构建和初始化.重载操作符.类描述.属性访问控制.自定义序列.反射机制.可调用对象.上下文管理.构建描述符对象 ...
- 用powermock 方法中new对象
在单元测试中有时需要对方法体内new出来的对象进行方法隔离,powermock提供了这个功能,下面是一个段样例代码: UserBean user = mock(UserBean.class, RETU ...