AJAX cross-domain request-JSONP get JSON data

Source: Internet
Author: User
Tags script tag vars

1. What is JSONP?

JSONP (JSON with Padding) is an unofficial protocol that allows the server-side integration of script tags back to the client to achieve cross-domain access in the form of JavaScript callback (this is simply a JSONP implementation form).

2. What is the use of JSONP?

Due to the limitations of the same-origin policy, XMLHttpRequest only allows resources to request the current source (domain name, protocol, port), in order to implement cross-domain requests, cross-domain requests can be implemented through the script tag, and then output JSON data on the server and execute callback functions to resolve cross-domain data requests.

3, how to use JSONP?

The demo below is actually a simple representation of JSONP, after the client declares the callback function, the client requests data across the domain through the script tag, then the server returns the corresponding data and executes the callback function dynamically.

HTML code (either):

HTML code
  1. <Meta content="text/html; Charset=utf-8 " http-equiv="content-type " />
  2. <script type="Text/javascript">
  3. function Jsonpcallback (Result) {
  4. alert (result);
  5. for (var i in result) {
  6. Alert (i+ ":" +result[i]);//loop output a:1,b:2,etc.
  7. }
  8. }
  9. var jsonp=document.createelement ("script");
  10. jsonp.type="Text/javascript";
  11. jsonp.src="Http://crossdomain.com/services.php?callback=jsonpCallback";
  12. document.getElementsByTagName ("Head") [0].appendchild (JSONP);
  13. </Script>
HTML code
  1. <Meta content="text/html; Charset=utf-8 " http-equiv="content-type " />
  2. <script type="Text/javascript">
  3. function Jsonpcallback (Result) {
  4. alert (RESULT.A);
  5. alert (result.b);
  6. alert (RESULT.C);
  7. for (var i in result) {
  8. Alert (i+ ":" +result[i]);//loop output a:1,b:2,etc.
  9. }
  10. }
  11. </Script>
  12. <script type="Text/javascript" src= "http://crossdomain.com/services.php?callback= Jsonpcallback "></script>

JavaScript links must be under function.

Server PHP Code (services.php):

PHP code
  1. <?php
  2. The service side returns JSON data
  3. $arr =Array (' A ' =>1,' B ' =>2,' C ' =>3,' d ' =>4,' e ' =>5);
  4. $result =json_encode ($arr);
  5. Echo $_get[' callback ']. ' ("hello,world!") ';
  6. Echo $_get[' callback ']. " ($result) ";
  7. Dynamic Execution callback function
  8. $callback =$_get[' callback ');
  9. echo $callback."  ($result) ";

If the above JS client code is implemented in the JQuery method,

$.getjson
$.ajax
$.get

How to implement the client JS code in jquery 1:

JS Code
  1. <script type="Text/javascript" src="jquery.js" ></script>
  2. <script type="Text/javascript" >
  3. $.getjson ("http://crossdomain.com/services.php?callback=?",
  4. function (Result) {
  5. For (var i in result) {
  6. Alert (i+":" +result[i]);   Loop output a:1,b:2,etc.
  7. }
  8. });
  9. </script>
  10. Jsonp principle:
    First register a callback with the client and then pass the callback name to the server.

    At this point, the server becomes JSON data.
    Then, in JavaScript syntax, a function is generated, and the function name is the parameter Jsonp passed up.

    Finally, the JSON data is placed directly into the function in the form of a parameter, so that a document of JS syntax is generated and returned to the client.

    The client browser parses the script tag and executes the returned JavaScript document, where the data is passed in as a parameter to the client's pre-defined callback function. (Dynamic execution callback function)

AJAX cross-domain request-JSONP get JSON data

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.