ajax跨域访问页面,并获得返回值(GET+Jsonp)
--页面 a.jsp:
<script type="text/javascript">
function test(){
$.ajax({
type:"GET",
timeout: 5000,
async: false,
dataType:"jsonp",
jsonp: "callbackparam",
jsonpCallback:"success_jsonpCallback",
url: "http://192.168.0.1:8080/test/b.jsp",
success:function(data){
alert(data[0].time);
},
error:function(XMLHttpRequest, textStatus, errorThrown){
alert("error");
}
});
}
</script>
--页面 b.jsp:
String callback = request.getParameter("callbackparam");
out.print(callback + "([{time:\"===\"}])");
--页面 a.jsp:
<script type="text/javascript">
function test(){
$.ajax({
type:"GET",
timeout: 5000,
async: false,
dataType:"jsonp",
jsonp: "callbackparam",
jsonpCallback:"success_jsonpCallback",
url: "http://192.168.0.1:8080/test/b.jsp",
success:function(data){
alert(data[0].time);
},
error:function(XMLHttpRequest, textStatus, errorThrown){
alert("error");
}
});
}
</script>
--页面 b.jsp:
String callback = request.getParameter("callbackparam");
out.print(callback + "([{time:\"===\"}])");