jquery异步请求实例

1、用ajax

jsp代码如下:

  
    
$.ajax({
type:
" get " ,
dataType:
" json " ,
url:
" <%=basePath%>actionSmUser.do?method=getUserMsgByUserId " ,
data:
" userId= " + userId,
success:
function (msg){
var data = eval( " ( " + msg.responseText + " ) " );
alert($(
" #userName " ).val());
$(
" #userName " ).val(data[ 0 ].userName);

}
});

2、用getJSON方式:

  
    
$.getJSON(url, function (data){
if (data != null && data != "" ){
$(
" #userIdInput " ).val(data[ 0 ].userId);
$(
" #userNameInput " ).val(data[ 0 ].userName);
$(
" #menusIdInput " ).val(data[ 0 ].menusId);
$(
" #depIdInput " ).val(data[ 0 ].depId);
$(
" #isValidInput " ).val(data[ 0 ].isValid);
$(
" #userIdInput " ).attr( " disabled " , true );
}

});
两种台Action返回字符串的组织形式可采用JSONArray array 的方式,返回array.toJSONString();

你可能感兴趣的:(jquery)