java中ajax使用--傳回一個物件
阿新 • • 發佈:2019-01-08
1、struts.xml配置
<!-- 查詢人員資訊 -->
<action name="validateDealper" class="sealBusinessAction" method="validateDealper">
<result type="json">
<param name="includeProperties">dealperson\.id,dealperson\.mobile,dealperson\.telephone,dealperson\.address</param>
</result>
</action>
2、action
// 判斷辦理人是否已存在
public String validateDealper() {
String dealper = getRequest().getParameter("dealper");
String dealpercode = getRequest().getParameter("dealpercode");
this.dealperson = this.sealbusinessService.Getdealperson(dealper,
dealpercode);
return SUCCESS;
}
private String jsonResultMsg = null;
public String getJsonResultMsg() {
return jsonResultMsg;
}
public void setJsonResultMsg(String jsonResultMsg) {
this.jsonResultMsg = jsonResultMsg;
}
3、js
var dealperson = msg.dealperson;獲得後臺查詢的物件。
$.ajax( {
type : "post" ,
url : "validateDealper.action",
dataType : "json",
data : {"dealper":getper,"dealpercode":code},
async : false,
cache : false,
success : function(msg) {
var dealperson = msg.dealperson;
if (dealperson != null) {
var id = dealperson.id;
var mobile = dealperson.mobile;
var telephone = dealperson.telephone;
var address = dealperson.address;
$("#dealpersonid").val(id);
$("#mobile").val(mobile);
$("#telephone").val(telephone);
$("#address").val(address);
}
}
});