客户端代码如下:
<%@ page language="java" import="java.util.*;" pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>测试跨域请求数据</title>
<script type="text/javascript" src="jquery/jquery.js"></script>
<script type="text/javascript">
$.getJSON("http://localhost:8080/test/server.jsp?jsoncallback=?",
function(data){
alert("ttt");
});
</script>
</head>
<body>
</body>
</html>
服务器端server.jsp代码如下:
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312"%>
<!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=gb2312">
<title>Insert title here</title>
</head>
<body>
<%
String callback = request.getParameter("jsoncallback");
out.println(callback + "({name:'sheng',sex:'男'})");
%>
</body>
</html>