标签:
使用jsonp跨域获取json数据。Ajax获取JAVA服务器json数据。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setHeader("Access-Control-Allow-Origin", "*");//开启CORS,支持跨域访问
//TODO
}
//$.ajax
$.ajax({
type:"get",
async:false,
url:"http://localhost:8080/AjaxServlet",
datatype:"jsonp",
jsonp: "callback",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(默认为:callback)
success:function(data){
alert(data);
},error:function(err){
alert("failed");
}
});
//$.getJSON
$.getJSON("http://localhost:8080/AjaxServlet?callback=?",function(result){
console.log(result);
});
<div ng-app="myApp" ng-controller="namesCtrl">
<p>循环对象:</p>
<ul>
<li ng-repeat="x in person | orderBy:‘country‘">
{{ x.Name + ‘, ‘ + x.Country }}
</li>
</ul>
</div>
<script type="text/javascript">
angular.module(‘myApp‘,[]).controller(‘namesCtrl‘,function($scope,$http){
$http.jsonp(‘http://localhost:8080/AjaxServlet?callback=JSON_CALLBACK‘).success(function(data){
$scope.person = data.records;
}).error(function(err){
alert(‘error:‘+err);
});
});
</script>
详细代码:http://download.csdn.net/detail/oofgdoo/9393325
标签:
原文地址:http://www.cnblogs.com/jkguo/p/5106621.html