jQuery中ajax请求的六种方法(三、四):$.getJSON()方法

4.$.getJSON()方法

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>jQuery中的ajax基础方法</title>
	</head>
	<script type="text/javascript" src="js/jquery-1.11.3.js"></script>
	<script type="text/javascript">
		$(document).ready(function() {
			$("#country").change(function() {
				// 拿到当前country中的值!
				var country = $(this).val();
				// 如果country 取到了值,并且不为空的时候。
				if (country != undefined && country != null) {
					// 根据国家获取该国的城市列表,并设置到城市下拉框中
					/*
						$.getJSON(url, [data], [callback]) 
						通过远程 HTTP GET 请求载入json信息;
						这是$.ajax的一个高级实现。
					*/
					$.getJSON( 
						// url
						"cityServlet", // 需要访问的url
						// data
						{"country":country}, // 传递给后台的数据
						// 回调函数
						function(data) {
							// 在确定data为未定义,或者为空的时候,就不继续往下执行了。
							if (data == undefined || data == null) {
								// 不知道可不可以这样使用return。
								return;
							}
							
							// 获取从服务器传过来的数据内容:其中的cities是服务器上规定的json的名字
							var cities = data.cities; // 直接的json对象
							// 清空原来的城市列表
							var $citySelect = $("#city"); // 约定jQuery的对象,命令都用$开头
						    $citySelect.empty();
							
							// 遍历cities,并且将其中的内容append到select中去
							$.each(cities, function(i, obj) {
								$citySelect.append("<option>"+obj.city+"</option>");
							});
						}
					);
				} else {
					// 未选择国家
					alert("请选择国家!");
				}
			});
		});
	</script>
	<body>
		<div style="width:100%;text-align: center;margin-top: 30px;">
	    	国家:<select id="country" style="width:160px;">
		    		<option>请选择</option>
		    		<option value="中国">中国</option>
		    		<option value="美国">美国</option>
	    		</select>
	    	&nbsp;&nbsp;---&nbsp;&nbsp;
	    	城市:<select id="city"></select>
    	</div>
	</body>
</html>

后台servlet:

package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/cityServlet")
public class CityServlet extends HttpServlet {

	private static final long serialVersionUID = -1046035703953361573L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String country = request.getParameter("country");
		String dataType = request.getParameter("dataType");
		/*
		String sendType = request.getParameter("sendType");
		if (!"post".equals(sendType)) { // 为get方式的时候,因为Tomcat7.0之前才有这个问题,忽略!
			country = new String(request.getParameter("country").getBytes("ISO-8859-1"), "utf-8");
		}
		*/
		StringBuffer sb = new StringBuffer("");
		if (!"xml".equals(dataType)) {
			sb.append("{");
			sb.append("\"cities\":[");
			if ("中国".equals(country)) {
				sb.append("{\"city\":\"北京\"},{\"city\":\"上海\"},{\"city\":\"广州\"},{\"city\":\"深圳\"}");
			} else if ("美国".equals(country)) {
				sb.append("{\"city\":\"华盛顿特区\"},{\"city\":\"纽约\"},{\"city\":\"洛杉矶\"},{\"city\":\"芝加哥\"}");
			}
			sb.append("]}");
			response.setContentType("text/html;charset=utf-8"); // 纯文本格式
		} else {
			sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root>");
			if ("中国".equals(country)) {
				sb.append("<city>北京</city>").append("<city>上海</city>").append("<city>广州</city>").append("<city>深圳</city>");
			} else if ("美国".equals(country)) {
				sb.append("<city>华盛顿特区</city>").append("<city>纽约</city>").append("<city>洛杉矶</city>").append("<city>芝加哥</city>");
			}
			sb.append("</root>");
			response.setContentType("text/xml;charset=utf-8"); // xml格式
		}
		PrintWriter out = response.getWriter();
		out.println(sb.toString());
		out.flush();
		
		out.close();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

猜你喜欢

转载自blog.csdn.net/qq_36791569/article/details/81067721