关于ajax接受json格式的数据二(使用jquery方式)
更新:HHH   时间:2023-1-7


<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

<script src="js/jquery-1.7.2.js"></script>

</head>


<body>

<form action="">

<select name="college" id="college" ></select>

</form>

<script>

$(function(){

$.get("jsonserver.jsp",function(data){

for(var i=0;i<data.length;i++)

$("#college").append(new Option(data[i].cname,data[i].value));

},"json");

})

</script>

</body>

</html>

jsonserver.jsp页面

<%@ page language="java" contentType="text/html; charset=Utf-8"

    pageEncoding="Utf-8"%>

 <%

  response.setHeader("Content-Type","text/xml");

  response.setHeader("charset","utf-8");

  String result ="[{\"value\":0,\"cname\":\"软件学院\"},{\"value\":\"1\",\"cname\":\"计算机学院\"}]";

System.out.println(result);

  out.print(result);

  out.flush();

  %>


返回web开发教程...