PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

js实现前端后台传送Json

php中世界最好的语言
php中世界最好的语言 原创
2018-04-20 17:30:26 2149浏览

这次给大家带来js实现前端后台传送Json,js实现前端后台传送Json的注意事项有哪些,下面就是实战案例,一起来看一下。

无论使用什么框架都存在着从controller向Html页面或者jsp页面传递数据的问题,最常用的方式是传递Json字符串。以前对这块知识有些模糊,现在整理一下。

【Jquery基本方法】

实现传值常用的是Jquery以及内部封装的ajax。首先看一下jquery的get()和post()语法。get()方法是从服务器获得数据,其主要参数就是获得后台请求地址,以及负责处理的回调函数

$.get(URL,callback);

$("button").click(function(){ 
 $.get("demo_test.php",function(data,status){ 
  alert("数据: " + data + "\n状态: " + status); 
 }); 
});

post通过HTTP post方法请求数据:

$.post(URL,data,callback);

$("button").click(function(){ 
  $.post("/try/ajax/demo_test_post.php", 
  { 
    name:" 撒", 
    url:"http://www.php.com" 
  }, 
    function(data,status){ 
    alert("数据: \n" + data + "\n状态: " + status); 
  }); 
});

【spring mvc框架+Jquery ajax】

spring mvc框架的controller通过标注方法向js返回Map类型参数。

@RequestMapping("update") 
@ResponseBody //此批注是ajax获取返回值使用 
public Map<string> update(Long num,BigDecimal amount){ 
  map<string> resultMap=new HashMap<string>(); 
   
  if(num==null || agentId==null || amount==null){ 
    resultMap.put("result","参数不合法"); 
    return resultMap; 
  } 
  resultMap.put("result",result); 
   
}</string></string></string>

jquery ajax获得返回值:

var params={}; 
params.num=num; 
params.id=id; 
params.amount=amount; 
$.ajax({ 
  async:false, 
  type:"post", 
  url:"uset/update", 
  data:params, 
  dataType:"json", 
  success:function(data){ 
    if(data.result=='success'){ 
      alert('修改成功'); 
    }else{ 
      alert('修改失败'); 
    } 
  }, 
  error:function(data){ 
    alert(data.result); 
  } 
   
})

如果在js中定义的参数与持久层定义的javabean保持一致,controller层同样可以接收实体。

【MUI绑定数据实例】

使用jquery很容易获得controller获得的json值,那我们如何操作json值,让其绑定到页面控件呢?首先我们简单理解一下json的结构:

var employees=[{"name":"Jon","age":12},{"name":"Tom","age":14}];

如上面定义的Json对象,{}表示对象,[]表示数组,"" 表示属性或值,: 表示后者是前者的值。

获得到json对象中的值:var name=employees[0].name;

修改:employees[0].name="LiMing";

MUI框架中的应用举例,实现list中添加li 标签:

mui.init();
var url="queryUser"
mui.ajax(url,{
	data:{
		'type':1,
		'limit':10
	},
	dataType:'json',
	type:'post',
	success:function(data){
		var songs=data.result.songs;
		var list=document.getElementById("list");
		var fragment=document.creeateDocumentFramgment();
		
		var li;
		mui.each(songs,function(index,item){
			var id=item.id,
			name=item.album.name,
			author=item.artists[0].name;
			
			li=document.createElement('li');
			li.className="mui-table-view-cell mui-media";
			li.innerHTML='<a>'+'<img>'+'<p>'+name+'</p><p>'+author+'</p>'+''+'</a>';
		fragment.appendChild(li);
		})
		
		list.appendChild(fragment);
		mui(document).imageLazyload({
			placeholder:'../img/60*60.gif';
		});
		
	},erro:function(xhr,type,errorThrown){
		console.log(type);
	}
	
});
//列表点击事件
mui("#list").on('tap','li a',function(){
	var id=this.getAttribute('id');
	var audio=this.getAttribute('data-audio');
	mui.openWindow({
		url:'music.html',
		id:'music.html',
		extras:{
			musicId:id,
			audioUrl:audio
		}
	});
});

【总结】

json格式的数据相对于xml文件来说,传输速度快且稳定,在前端设计中是一种非常不错的选择。

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

将带有html字段字符串转换为HTML标签

怎么用Post方法传递json参数

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。