Jquery Ajax 调用后台并返回数据

一、前台调用ajax并解析json对象。

		$.ajax({
							url : '',
							type : 'POST', //GET
							data : '’,
							beforeSend : function(request) {
								//调用ajax之前
							},
							success : function(data) {
								var obj = JSON.parse(data); 
								if (obj.viewPdf == 1) {
									//处理....
								} else if (obj.viewPdf == 2) {
									layer.msg('预览文件转换错误!', {
										icon : 2,
										time : 2000
									});
								} else {
									layer.msg('预览失败!', {
										icon : 2,
										time : 2000
									});
								}
							}
						});

  

二、后台处理

	public void convert2Pdf(HttpServletRequest request,HttpServletResponse response) {
		PrintWriter out = null;
		try {
			out = response.getWriter();
			JsonObject objJson = new JsonObject();
			objJson.addProperty("viewPdf", 1);
			objJson.addProperty("pdfFile",2);
			out.print(objJson.toString());
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

猜你喜欢

转载自www.cnblogs.com/ywx2/p/Ajax.html