servlet combined with jquery to achieve asynchronous request

Part of the servlet code, depends on the net.sf.json package
1. Returns a json object
//Initialize the query condition watershed
			HashMap<String,ArrayList<HashMap>> resultMap = new HashMap<String,ArrayList<HashMap>>();
			ArrayList<HashMap> lyList = null;//流域
			try {
				String lySQL = "select distinct(syslycode),syslyname from t_inventoryList where syslycode is not null";
				lyList = ab.query(lySQL);
				resultMap.put("ly", lyList);
				JSONObject jsonObj = JSONObject.fromObject(resultMap);
				toRespdb(response,jsonObj);
			} catch (Exception e) {
				e.printStackTrace ();
			}

2. Return the json array
try {
				QJMNUtil util=new QJMNUtil();
				List<Map<String,Object>> seriesList = util.getFushionContentForHYPFFX(request,ab);
				JSONArray jsonArray = JSONArray.fromObject(seriesList);
				toRespdb(response,jsonArray);
				}
			catch (Exception e) {
				e.printStackTrace ();
			}

/**
	 * Write data to the foreground
	 * @param resp
	 * @param content
	 */
	public void toRespdb(HttpServletResponse resp,Object content){
		try {
			PrintWriter out = resp.getWriter();
			out.print(content);
			out.close();
		} catch (IOException e) {
			e.printStackTrace ();
		}
	}

Front-end jquery part jsonarray
//When changing the watershed, modify the data of the river drop-down box
function modifyhl(obj){
	var lyval = $ (obj) .val ();
	// empty the river text box when changing the watershed
	$("#hllist").val("");
	$("input:hidden[name='wsystemcode3']").val("");
	$.ajax({
		url: '../qdfx.do',
        cache: false, //Do not fetch data from cache
        data: {todo:'QueryByHl',lycode:lyval,type:'eidt'},
        type:'post',
        async:false,
        dataType:'json',
        timeout: 100000,//timeout
        error:function (msg) {
            alert(msg.responseText);
        },
        success:function(dbresp)
        {
        	$("#loadId").remove();
        	$.fn.zTree.init($("#treeTrade"), setting,dbresp);
        }
	});
}

Front-end jquery part jsonobject
// load watershed
function loadly(){
	$.ajax({
		url: '../qdfx.do',
        cache: false, //Do not fetch data from cache
        data: {todo:'QueryByLy'},
        type:'post',
        async:false,
        dataType:'json',
        timeout: 100000,//timeout
        error:function (msg) {
            alert(msg.responseText);
        },
        success:function(dbresp)
        {
        	var lyList = dbresp.ly;
        	var trade = dbresp.trade;
        	for(i=0;i<lyList.length;i++){
        		$("#lylist").append("<option value='"+lyList[i].SYSLYCODE+"'>"+lyList[i].SYSLYNAME+"</option>");
        	}
        }
	});
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326880644&siteId=291194637