ajax 用法 + json使用方法

前端之家收集整理的这篇文章主要介绍了ajax 用法 + json使用方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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' onclick='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(); } }

原文链接:https://www.f2er.com/ajax/162610.html

猜你在找的Ajax相关文章