ajax写法(初始化、异步提交、表单提交)

1、//初始载入 

$(function(){
       
    });


   2、  //ajax异步请求
    $.ajax({
        url:'www.sina.com',
        data:{a:'1'},
        type:'post',
        dataType:'json',
        cache:false,
        success:function(data){
            alert(data);
        },
        error:function(data){

        }
    });

或者jquery.ajax({

   ...

}

);

 -------------------------

3、AJAX Submit提交

     需要引入jquery.form.js <script type="text/javascript" src="${root}/resources/js/jquery.form.js"></script>

     $("#btn").click(function(){
            alert(23);
            $("#myconfigForm").ajaxSubmit(
                    {
                url: '${root}/requestWap/wapCashBox/myCashBill',
                type: "post",
                dataType: "text/html",
                success: function (data) {
                    alert(data);
                },
                error: function () {
                    alert("保存出错!");
                    return false;
                }


            });
            alert(34);
        });

<form id="myconfigForm">
    <table>
        <tr><td> 标题:<input type="text" name="title" /></td></tr>
        <tr><td> 内容:<textarea name="content"></textarea></td></tr>
        <tr><td> <input type="button" id="btn" value="提交"/></td></tr>

    </table>
</form>

4、Spring  @ResponseBody  ajax返回值

 @RequestMapping(value="/roleresourcedata")
    public @ResponseBody String roleresourcedata(ModelMap model, HttpServletRequest request, HttpServletResponse response) throws Exception {
       /* StringBuilder jsonObj= new StringBuilder();
        jsonObj.append("[{").append("id:").append(1).append(",text:").append("角色授权").append("}]");
        logger.info("strb.toString() ==" + jsonObj.toString());*/
        net.sf.json.JSONObject jsonObj = new net.sf.json.JSONObject();
        String result="1";
        jsonObj.put("id", result);
        return jsonObj.toString();
    }

$(function(){
        $.ajax({
            type:"POST",
            url:'${ctx}/manage/role/roleresourcedata',
            async : false,
            cache:false,
            dataType:'json',
            success:function(data){
              var data=eval(data);
               alert(data.id);
            },errot:function(data){
                alert(22);
            }
        });

    });

-----注意:如果 dataType:'String', 则不需要eval(data)进行解析,直接输出。

相关推荐