ajax例子

来源:互联网 发布:部落冲突升级7本数据 编辑:程序博客网 时间:2024/04/28 12:03
$.ajax({type : "POST", url: '${pageContext.request.contextPath}/marketAction!checkByRole.action',async: true,data : {userId : '<%=userId%>',timestamp : getTimestamp()},dataType: 'json',success: function(msg) {if(msg == 0) {$.messager.alert('Warning','你的套餐选择不能添加更多的商场!');  return;}                                       }        });



后台代码,即marketAction中的代码可以这么写:

    public void checkByRole()    {        ********        ********       if (x >= market_num)        {            super.writeJson(0);        }        else        {            super.writeJson(1);        }            }

引入阿里巴巴的fastjson的jar包文件、

public class BaseAction {    /** * 将对象转换成JSON字符串,并响应回前台 *  * @param object * @throws IOException */public void writeJson(Object object) {try {String json = JSON.toJSONStringWithDateFormat(object, "yyyy-MM-dd HH:mm:ss");ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");ServletActionContext.getResponse().getWriter().write(json);ServletActionContext.getResponse().getWriter().flush();ServletActionContext.getResponse().getWriter().close(); } catch (IOException e) {e.printStackTrace();}}}


0 0
原创粉丝点击