Java Web get requests cross-domain

Source: Internet
Author: User

http://hw1287789687.iteye.com/blog/2188617

How do I cross-domain requests in the Java Web?

For more information, please refer to Jsonp: http://json-p.org/

The page code is as follows:

HTML code
  1. <! DOCTYPE HTML>
  2. <html>
  3. <head lang="en">
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <script type="Application/javascript" >
  7. function Jsonpcallback (Result) {
  8. Alert (json.stringify (result));
  9. /*for (var i in result) {
  10. Alert (i+ ":" +result[i]);//loop output a:1,b:2,etc.
  11. }  */
  12. }
  13. var jsonp=document.createelement ("script");
  14. jsonp.type="Text/javascript";
  15. jsonp.src="Http://192.168.0.100:8080/tv_mobile/video/text2?callback=jsonpCallback";
  16. document.getElementsByTagName ("Head") [0].appendchild (JSONP);
  17. </Script>
  18. </head>
  19. <body>
  20. </body>
  21. </html>

Effects that are accessed in the browser:

The background uses spring MVC:

Java code
  1. @ResponseBody
  2. @RequestMapping (value = "/text2", Produces=systemhwutil.response_contenttype_javascript2)
  3. Public String Text2 (httpservletrequest request, HttpServletResponse response,string Contenttype2,string callback)
  4. throws IOException {
  5. String content = null;
  6. Map map = new HashMap ();
  7. Map.put ("FileName", "a.txt");
  8. CONTENT=JSONPUTIL.GETJSONP (map, callback);
  9. SYSTEM.OUT.PRINTLN (content);
  10. return content;
  11. }

The Jsonputil.getjsonp static method is implemented as follows:

Java code
  1. /***
  2. * For JSONP calls
  3. * @param map: Used to construct JSON data
  4. * @param callback: The JavaScript method name of the callback
  5. * @return
  6. */
  7. public static String Getjsonp (Map map,string callback)
  8. {
  9. Objectmapper mapper = new Objectmapper ();
  10. String content = null;
  11. try {
  12. Content = mapper.writevalueasstring (map);
  13. SYSTEM.OUT.PRINTLN (content);
  14. } catch (Jsongenerationexception e) {
  15. E.printstacktrace ();
  16. } catch (Jsonmappingexception e) {
  17. E.printstacktrace ();
  18. } catch (IOException e) {
  19. E.printstacktrace ();
  20. }
  21. if (Valuewidget.isnullorempty (callback)) {
  22. return content;
  23. }
  24. return callback+"(" +content+")";
  25. }

Rely on the Jackson Library

What is returned in the background is:jsonpcallback ({"FileName": "A.txt"})

Content Type is

Note: The form returned in the background is the function name ( parameter), where the function name is the name of the callback function

Reference:

Spring MVC sets the content type of the responder body

AJAX cross-domain request-JSONP get JSON data:http://justcoding.iteye.com/blog/1366102

APP Framework Send JSONP request (3): http://hw1287789687.iteye.com/blog/2190719

Java Web get request cross-domain (GO)

Related Article

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.