這次帶給大家js實作前後台Json互相傳送,js實作前後台Json互相傳送的注意事項有哪些,下面就是實戰案例,一起來看一下。
【Jquery基本方法】
實作傳值常用的是Jquery以及內部封裝的ajax。首先來看看jquery的get()和post()語法。 get()方法是從伺服器取得數據,其主要參數就是取得後台請求位址,以及負責處理的回呼函數:
$.get(URL,callback);
1 2 3 4 5 | $( "button" ).click( function (){
$.get( "demo_test.php" , function (data,status){
alert( "数据: " + data + "\n状态: " + status);
});
});
|
登入後複製
post透過HTTP post方法請求資料:
$.post(URL,data,callback);
1 2 3 4 5 6 7 8 9 10 | $( "button" ).click( function (){
$.post( "/try/ajax/demo_test_post.php" ,
{
name: "菜鸟教程" ,
url: "http://www.runoob.com"
},
function (data,status){
alert( "数据: \n" + data + "\n状态: " + status);
});
});
|
登入後複製
#【spring mvc框架 Jquery ajax】
spring mvc框架的controller透過標註方法向js傳回Map類型參數。
1 2 3 4 5 6 7 8 9 10 11 12 | @RequestMapping( "update" )
@ResponseBody
public Map<String,Object> update(Long num,BigDecimal amount){
map<string,Object> resultMap= new HashMap<string,Object>();
if (num==null || agentId==null || amount==null){
resultMap.put( "result" , "参数不合法" );
return resultMap;
}
resultMap.put( "result" ,result);
}
|
登入後複製
jquery ajax獲得回傳值:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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的結構:
1 | var employees=[{ "name" : "Jon" , "age" :12},{ "name" : "Tom" , "age" :14}];
|
登入後複製
如上面定義的Json對象,{}表示對象,[]表示數組,"" 表示屬性或值,: 表示後者是前者的值。
得到到json物件中的值:var name=employees[0].name;
修改:employees[0].name ="LiMing";
MUI框架中的應用範例,實作list中新增li 標籤:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | 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 class="mui-navigate-right" id='+ id +' data-audio='+ audio +'>' + '<img class="mui-media-object mui-pull-left" srcload="'+picUrl+'">' + '<p class="mui-media-body">' +name+ '<p class="mui-ellipsis">' +author+ '</p>' + '</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中文網其它相關文章!
推薦閱讀:
Vue實作Observer步驟詳解
vue父元件呼叫子元件方法總結
以上是js實作前後台Json互相傳送的詳細內容。更多資訊請關注PHP中文網其他相關文章!