Use JSONP to get JSON data across domains. Ajax gets the Java server JSON data.
In the servlet code:
protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { C0/>response.setheader ("Access-control-allow-origin", "*");//Open cors, support cross-domain access //todo}
HTML code: Jquery.ajax or Jquery.getjson
$.ajax$.ajax ({type: "Get", Async:false, url: "Http://localhost:8080/AjaxServlet", DataType: "Jsonp", Jsonp: "Callbac K ",///pass to the request handler or page, to obtain the name of the JSONP callback function name (default: Callback) success:function (data) {alert (data); },error:function (ERR) {alert ("failed"); }});//$.getjson $.getjson ("http://localhost:8080/AjaxServlet?callback=?", function (Result) {Console.log ( result); });
Angulgrjs's $http.jsonp
<div ng-app= "myApp" ng-controller= "Namesctrl" ><p> Loop object:</p><ul> <li ng-repeat= "x in person | By: ' 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>
Detail code: http://download.csdn.net/detail/oofgdoo/9393325
Ajax + Java cross-domain access JSON