alert("我是蓝色的车");
<html>
<head>
<title>JSONP演示1-请首先启动bos_map_jsonp项目</title>
<meta charset="utf-8" />
<script type="text/javascript"
src="http://localhost:9004/bos_map_jsonp/car1/blueCar.js"></script>
</head>
<body>
</body>
</html>
<html >
<head>
<title>jsonpdemo2</title>
<meta charset="utf-8" />
<script type="text/javascript">
function myOwnFun(data){
alert("从jsonp项目请求的数据:" + data.id + "-" + data.value);
}
</script>
<script type="text/javascript" src="http://localhost:9004/bos_map_jsonp/car2/redCar.js">
</script>
</head>
<body>
</body>
</html>
myOwnFun({"id":1,"value":"我是红色的车"})
<html >
<head>
<title>jsonpdemo3</title>
<meta charset="utf-8" />
<script type="text/javascript">
function myOwnFun(data){
alert("从jsonp项目请求的数据:" + data.id + "-" + data.value);
}
</script>
<script type="text/javascript" src="http://localhost:9004/bos_map_jsonp/carInfo?callback=myOwnFun"></script>
</head>
<body>
</body>
</html>
public class JsonpServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String callback = req.getParameter("callback");
String resultStr = callback + "({\"id\":2,\"value\":\"我是兰博基尼车\"})";
//myOwnFun({id:2,value:我是兰博基尼})
resp.setCharacterEncoding("utf-8");
resp.getWriter().write(resultStr);
}
}
<html>
<head>
<title>jsonpdemo2</title>
<meta charset="utf-8" />
<script type="text/javascript" src="../pickaddr/jquery.js"></script>
<script type="text/javascript">
//依旧请求demo3中的请求路径
$.getJSON("http://localhost:9004/bos_map_jsonp/carInfo?callback=?",
{},
function(data){
alert(data.id + data.value);
});
</script>
</head>
<body>
</body>
</html>
195.22 KB, 下载次数: 32
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |