码迷,mamicode.com
首页 > Web开发 > 详细

ajax与后台交互传输数据的工具类

时间:2016-08-23 16:17:44      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

public class Result<T> implements Serializable {

    private static final long serialVersionUID = 3637122497350396679L;

    private boolean success;
    private T data;
    private String msg;

    public Result() {
    }

    public Result(boolean success) {
        this.success = success;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Result(boolean success, String msg) {
        super();
        this.success = success;
        this.msg = msg;
    }

    public Result(boolean success, T data) {
        super();
        this.success = success;
        this.data = data;
    }

}

该类为前后台交互提供了非常大的便捷:

下面是前后台的ajax交互:

前台ajax代码:

$.ajax({
            url: "<%=request.getContextPath()%>/supp/deleteSupp",
            data : {supplierId:supplierId},
            async: false, //请求是否异步,默认为异步,这也是ajax重要特性
            type: "GET",   //请求方式
            success: function(data) {
                var rs = eval(‘(‘+data+‘)‘);
                flag = rs.success;
                if(flag){
                    alert("删除成功!");
                }
            }
        });

下面是后台java代码:

    @RequestMapping("/deleteSupp")
    @ResponseBody
    public Result<String> deleteSupplier(HttpServletRequest request){
        Result<String> rs = new Result<>();
        String supplierId = request.getParameter("supplierId");
        supplierService.deleteSupplierById(supplierId);
        rs.setSuccess(true);
        return rs;
    }

 

ajax与后台交互传输数据的工具类

标签:

原文地址:http://www.cnblogs.com/blog411032/p/5799669.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!