關於如何接受異步ajax請求返回前臺的數據

1、第一種方式通過bean,後臺需封裝成json數據格式,前臺paseJSON();

public void checkCode() {

    ResponseMessage rm = new ResponseMessage(true,null);

    String hql = "from TblWrPeople t where t.wrPeopleCode='"+code+"' and t.delFlag='"+Constants.DELFLAGA+"'";

    List<TblWrPeople> tblWrPeopleList = (List<TblWrPeople>)this.workResService.find(hql);

    if (tblWrPeopleList != null && tblWrPeopleList.size() > 0) {

rm.setSuccess(false);

rm.setErrorMsg("數據庫中已存在該代號,請重新錄入");

this.responseAJAX(JSONObject.fromObject(rm).toString(), getResponse());

}

}

前臺獲取:

function checkCode() {

var code = $("#wrPeopleCode").val();

$.post('workRes_checkCode.action',{"code":code},function(result){

result = $.parseJSON(result);

if (result.success == false){

                 alert(result.errorMsg);

                 $("#wrPeopleCode").val('');

             }

});

}

2、後臺直接返回一個String類型的字符串,前臺的話不再需要parseJSON,直接獲取result即可;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章