zoukankan      html  css  js  c++  java
  • 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);
                      }
                  });            
  • 相关阅读:
    [GIT] warning: LF will be replaced by CRLF问题解决方法
    最近想学的工具
    如何在webstrom中配置eslint和less
    Git常用命令
    windows下nginx安装、配置与使用
    关于 addEventListener 和 handleEvent 方法
    PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup, __toStr
    Git使用详细教程
    9个永恒的UI设计原则
    常见浏览器兼容性问题与解决方案
  • 原文地址:https://www.cnblogs.com/fdzfd/p/5690916.html
Copyright © 2011-2022 走看看