jQuery中的Ajax

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

(4)表单序列化提交

              var data = $("#formobjNew").serialize();
              var url = "weixinArticleController.do?doUpdate";
              $.ajax({
                  url:url,
                  data:data,
                  type:'post',
                  dataType:"JSON",
                  success:function(data){
                      if(data.success){
                          parent.refresh();
                      }
                      tip(data.msg);
                  }
              });            

 

posted @ 2016-07-21 10:48  ws珍惜现在  阅读(409)  评论(0编辑  收藏  举报