標籤:
一:$.ajax()
(1)在jQuery中$.ajax()方法屬於最底層的方法。它的結構為:$.ajax(options)
(2)樣本
var url = "voteRankingController.do?getApplyNum&voteId=${voteId}"; $.ajax({ url:url, type:"GET", dataType:"JSON", success:function(data){ if(data.success){ //tip(data.msg); $("#applynum").text(data.obj); }else{ //tip(data.msg); } } });
其中,data是後台返回的自訂AjaxJson格式
/** * $.ajax後需要接受的JSON * * @author * */public class AjaxJson { private boolean success = true;// 是否成功 private String msg = "操作成功";// 提示資訊 private Object obj = null;// 其他資訊 private Map<String, Object> attributes;// 其他參數 public Map<String, Object> getAttributes() { return attributes; } public void setAttributes(Map<String, Object> attributes) { this.attributes = attributes; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Object getObj() { return obj; } public void setObj(Object obj) { this.obj = obj; } public boolean isSuccess() { return success; } public void setSuccess(boolean success) { this.success = success; } public String getJsonStr(){ JSONObject obj = new JSONObject(); obj.put("success", this.isSuccess()); obj.put("msg", this.getMsg()); obj.put("obj", this.obj); obj.put("attributes", this.attributes); return obj.toJSONString(); }}View Code
(3)當後台返回的是list資料時
$(document).ready(function(){ var url = "researchController.do?getAllProblem&iid=${iid}"; $.ajax({ url:url, type:"GET", dataType:"JSON", success:function(data){ for(var question in data){ //alert(data[question][‘option‘]); for(var item in data[question][‘option‘]){ var oid = data[question][‘option‘][item][‘id‘]; var ocontent = data[question][‘option‘][item][‘content‘]; } } }); });//end of ready
其中,後台getAllProblem方法返回的是List<QuestionEntity>,QuestionEntity實體中包含List<OptionEntity> option實體。
jQuery中的Ajax