JQUERY+AJAX+JSONP Cross-domain access

Source: Internet
Author: User

JSONP (JSON with Padding) is a "usage pattern" of data format JSON that allows Web pages to obtain data from other domains.

For more detailed information on JSONP, please refer to http://baike.baidu.com/view/2131174.htm, below is an example:

I. Client

  1. <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <Meta http-equiv= "content-type" content= "text/html; Charset=utf-8 ">
  5. <title>insert title here</title>
  6. <script type="Text/javascript" src="Resource/js/jquery-1.7.2.js"></ Script>
  7. </head>
  8. <script type="Text/javascript">
  9. $ (function () {
  10. /*
  11. Abbreviated form, the effect is the same
  12. $.getjson ("Http://app.example.com/base/json.do? sid=1494&busiid=101&jsonpcallback=? ",
  13. function (data) {
  14. $ ("#showcontent"). Text ("Result:" +data.result)
  15. });
  16. */
  17. $.ajax ({
  18. Type: "Get",
  19. Async:false,
  20. URL: "Http://app.example.com/base/json.do?" sid=1494&busiid=101 ",
  21. DataType: "Jsonp",//data type is JSONP
  22. JSONP: "Jsonpcallback",//parameter for the server to receive the function name of the callback call
  23. Success:function (data) {
  24. $ ("#showcontent"). Text ("Result:" +data.result)
  25. },
  26. Error:function () {
  27. Alert (' fail ');
  28. }
  29. });
  30. });
  31. </Script>
  32. <body>
  33. <div id="showcontent">result:</div>
  34. </body>
  35. </html>

Two. Server-side

  1. Import java.io.IOException;
  2. Import Java.io.PrintWriter;
  3. Import Java.util.HashMap;
  4. Import Java.util.Map;
  5. Import Javax.servlet.http.HttpServletRequest;
  6. Import Javax.servlet.http.HttpServletResponse;
  7. Import Net.sf.json.JSONObject;
  8. Import Org.springframework.stereotype.Controller;
  9. Import org.springframework.web.bind.annotation.RequestMapping;
  10. @Controller
  11. Public class Exchangejsoncontroller {
  12. @RequestMapping ("/base/json.do")
  13. public void Exchangejson (httpservletrequest request,httpservletresponse response) {
  14. try {
  15. Response.setcontenttype ("Text/plain");
  16. Response.setheader ("Pragma", "No-cache");
  17. Response.setheader ("Cache-control", "No-cache");
  18. Response.setdateheader ("Expires", 0);
  19. map<string,string> map = new hashmap<string,string> ();
  20. Map.put ("result", "content");
  21. PrintWriter out = Response.getwriter ();
  22. Jsonobject Resultjson = jsonobject.fromobject (map); //Assemble JSON as needed
  23. String jsonpcallback = Request.getparameter ("Jsonpcallback"); Client Request Parameters
  24. Out.println (jsonpcallback+"(" +resultjson.tostring (1,1) +")"); Return JSONP Format Data
  25. Out.flush ();
  26. Out.close ();
  27. } catch (IOException e) {
  28. E.printstacktrace ();
  29. }
  30. }
  31. }

JQUERY+AJAX+JSONP Cross-domain access

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.