JQuery + ajax + jsonp cross-origin access, jqueryjsonp

Source: Internet
Author: User

JQuery + ajax + jsonp cross-origin access, jqueryjsonp

Jsonp (JSON with Padding) is a "usage mode" in json format that allows webpages to obtain information from other domains.

For more information about Jsonp, see http://baike.baidu.com/view/4101174.htm. The following is an example:

 

I. Client

Html code
  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, with the same effect
  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", // the data type is jsonp
  22. Jsonp: "jsonpCallback", // parameters of the function name used by the server to receive callback calls
  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>

Ii. Server Side

Java code
  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 () + ")"); // return jsonp format data
  25. Out. flush ();
  26. Out. close ();
  27. } Catch (IOException e ){
  28. E. printStackTrace ();
  29. }
  30. }
  31. }

 


Jquery ajax can obtain json data through cross-origin, but an error is returned when json parsing. How can this problem be solved?

It seems that the jsonp access method is not provided. weather.com.cn supports the iframe method.

The jsonp type cross-Origin data of jquery ajax is used, and the returned data is never obtained.

Js error: SyntaxError: missing; before statement will be reported on the page.
See where the Chinese Semicolon is used. In addition, the error information is printed out in the error file.
Console. log (r)
 

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.