jQuery中的Ajax

來源:互聯網
上載者:User

標籤:

一:$.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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.