ajax 用法 + json使用方法

关于Struts2 spring + hibernate进Q群: 130529143交流。
有偿技术支持Q群:
398162181 



1前台js:


/*
ctrlType=1:查询产品类别
ctrlType=2:查询解决方案类别
flag="delete":删除productId的产品
flag="add":新增typeId类的产品
flag="select":查询typeId的所有产品
*/
var globalCtrlType;

function showTypeList(flag, typeId, ctrlType){
var typeName = "";
var parentId;
if($("#type").val() != "" && $("#type").val() != null){
var parentId = $("#type").val();
}else{
parentId = 1;
}
//alert(parentId);
if(flag == "add"){
if(parentId == 0){
alert("请选择父类名称");
return false;
}

typeName = $("#typeName").val();
if(typeName == "" || typeName == "请输入如类别名称后点击✔"){
alert("请输入需添加的类别名称 !");
return false;
}
ctrlType = globalCtrlType;

$.ajax({
type : "POST",
url : "manage_type.action",
data : "flag=" + flag + "&typeId=" + typeId + "&typeName=" + typeName + "&ctrlType=" + ctrlType+ "&parentId=" + parentId,
dataType : "text",
success : function(data) {
$("#list").children("div:last").html("");
//将字符串转为JSON对象
var obj = eval('(' + data + ')');
var stringTypeList = obj.stringTypeList;
var stringAllTypeList = obj.stringAllTypeList;
var length = stringTypeList.length;
globalCtrlType = stringTypeList[0].ctrlType;
//拼html
var _html = "<ul>";
for(var i = 0; i < length; i++){
  _html += "<li class='productList link' id=\"" + stringTypeList[i].id + "\">" + (i+1) + "." + stringTypeList[i].name + "</li>" + 
           "<li class='productDelBtn link' οnclick='showTypeList(" + "\"delete\"," + stringTypeList[i].id + "," + stringTypeList[i].ctrlType + ");'>✘</li>" ;
}
_html += "</ul>";
//将_html加入div
$("#list").children("div:last").html(_html);

$("#list").find("select").html("");
//将字符串转为JSON对象
var lengthAll = stringAllTypeList.length;
//拼html
var _htmlAll = "";
for(var i = 0; i < lengthAll; i++){
  _htmlAll += "<option value=\"" + stringAllTypeList[i].id + "\">" + stringAllTypeList[i].name + "</option>" 
}
//将_html加入div
$("#list").find("select").html(_htmlAll);

}
});
}



2后台信息接收:


 /**
     * 获取给定种类的所有产品
       flag="delete":删除productId的产品
       flag="add":新增typeId类的产品
       flag="select":查询typeId的所有产品
     */
    public void product_list()
    {
        String flag = ((String[])ActionContext.getContext().getParameters().get("flag"))[0];
        int proId = Integer.parseInt(((String[])ActionContext.getContext().getParameters().get("proId"))[0]);
        int typeId =  Integer.parseInt(((String[])ActionContext.getContext().getParameters().get("typeId"))[0]);
        String proName = ((String[])ActionContext.getContext().getParameters().get("proName"))[0];
        if(0 != proId)
        {
            try
            {
                productService.delete(proId);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        else if(flag.equals("add"))
        {
            Product product = new Product();
            product.setName(proName);
            product.setTypeId(typeId);
            productService.save(product);
        }
        List<Product> productList = productService.getProductList(1,typeId);
        StringBuffer stringb = new StringBuffer("[");
        for(int i = 0; i < productList.size(); i++)
        {
            stringb.append("{");
            stringb.append("id:\"" + productList.get(i).getId() + "\"," );
            stringb.append("typeId:\"" + productList.get(i).getTypeId() + "\"," );
            stringb.append("name:\"" + productList.get(i).getName() + "\"," );
            stringb.append("presentation:\"" + productList.get(i).getPresentation() + "\"," );
            stringb.append("path:\"" + productList.get(i).getPath() + "\"," );
            stringb.append("status:\"" + productList.get(i).getStatus() + "\"" );
            stringb.append("}");
            
            if(i < productList.size() - 1)
            {
                stringb.append(",");
            }
        }
        
        stringb.append("]");
        try
        {
            getResponse().getWriter().print(stringb.toString());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        
    }


关于Struts2 spring + hibernate进Q群: 130529143交流。
有偿技术支持Q群:
398162181 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值