Ajax请求+JSON传回到页面数据的例子

前端之家收集整理的这篇文章主要介绍了Ajax请求+JSON传回到页面数据的例子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

前台页面(简略)

<span style="font-size:18px;"><span style="white-space:pre">		</span><tr>
			<td height="32px" width="150px" align="center" bgcolor="#eaf8ff">机构编号</td>
			<td>
			<input type="text" name="" id="" size="40" onblur="getJgmc(this.value)"/>
			<input type="hidden" name="nlyz.jdjg_id" id="jdjg_id"/>
			</td>
		</tr></span>
javaScrip代码

function getJgmc(xkzh){
	$.ajax({
		method : "POST",url : "jdjg/jdjg_getJgmcByXkzh.do",data : "xkzh="+xkzh,dataType : "json",cache : false,error : function() {
				alert("请求失败");
			},success : function(data) {
	        	var jgmc = data.jgmc;
	        	var jdjg_id = data.jdjg_id;
	        	var xzqy = data.xzqy;
	        	if(jgmc == "null"){
	        		alert("无对应机构,请核准机构编号!");
	        	}else{
	        		document.getElementById("jgmc").value = jgmc;
	        		document.getElementById("jdjg_id").value = jdjg_id;
	        		document.getElementById("xzqy").value = xzqy;
	        	}
			}
	});
	

后台数据传输实现
public void getJgmcByXkzh() throws IOException {
		jdjg = jdjgService.getJgmcByXkzh(request.getParameter("xkzh"));
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		JSONObject jo = new JSONObject();
		if (jdjg!=null) {
			jo.put("jgmc",jdjg.getJg_jgmc());
			jo.put("jdjg_id",jdjg.getId());
			jo.put("xzqy",jdjg.getJg_xzqy());
		}else {
			jo.put("jgmc","null");
		}
		out.print(jo);
		out.flush();
        out.close();
}

猜你在找的Ajax相关文章