Ajax + Java cross-domain access JSON

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.