[04] JSONP tutorial, 04jsonp tutorial

Source: Internet
Author: User

[04] JSONP tutorial, 04jsonp tutorial
JSONP tutorial

Jsonp (JSON with Padding) is a "usage mode" of json that allows a webpage to retrieve information from another domain name (website), that is, to read data across domains.

Why do we need a special technology (JSONP) to access data from different domains (websites? This is because of the same-origin policy.

Same-origin policy, a well-known security policy proposed by Netscape, is now used by all browsers that support JavaScript.

 

 

JSONP Application 1. Server JSONP format data

If the customer wants to access: http://www.baidu.com/try/ajax/jsonp.php? Jsonp = callbackFunction.

Assume that the customer expects to return JSON data: ["mermername1", "customername2"].

The data actually returned to the client is displayed as callbackFunction (["mermername1", "customername2"]).

The jsonp. php code of the Server File is:

  1. <?php
  2. header('Content-type: application/json');
  3. // Obtain the callback function name
  4. $jsoncallback = htmlspecialchars($_REQUEST ['jsoncallback']);
  5. // Json data
  6. $json_data ='["customername1","customername2"]';
  7. // Output data in jsonp format
  8. echo $jsoncallback ."(". $json_data .")";
  9. ?>
2. The client implements the callbackFunction function.
  1. <script type="text/javascript">
  2. function onCustomerLoaded(result, methodName)
  3. {
  4. var html ='<ul>';
  5. for(var i =0; i < result.length; i++)
  6. {
  7. html +='<li>'+ result[i]+'</li>';
  8. }
  9. html +='</ul>';
  10. document.getElementById('divCustomers').innerHTML = html;
  11. }
  12. </script>
Page display
<div id="divCustomers"></div>

  

Complete client Page code
  1. <!DOCTYPE html >
  2. <Title> JSONP instance </title>
  3. <body>
  4. <div id="divCustomers"></div>
  5. <script type="text/javascript">
  6. function callbackFunction(result, methodName)
  7. {
  8. var html ='<ul>';
  9. for(var i =0; i < result.length; i++)
  10. {
  11. html +='<li>'+ result[i]+'</li>';
  12. }
  13. html +='</ul>';
  14. document.getElementById('divCustomers').innerHTML = html;
  15. }
  16. </script>
  17. <script type="text/javascript" src="http://www.baidu.com/try/ajax/jsonp.php?jsoncallback=callbackFunction"></script>
  18. </body>
JQuery uses JSONP

The above code can be used as an example of jQuery code:

  1. <!DOCTYPE html>
  2. <Title> JSONP instance </title>
  3. <script src="http://apps.bdimg.com/libs/jquery/1.8.3/jquery.js"></script>
  4. <body>
  5. <div id="divCustomers"></div>
  6. <script>
  7. $.getJSON("http://www.baidu.com/try/ajax/jsonp.php?jsoncallback=?", function(data){
  8. var html ='<ul>';
  9. for(var i =0; i < data.length; i++)
  10. {
  11. html +='<li>'+ data[i]+'</li>';
  12. }
  13. html +='</ul>';
  14. $('#divCustomers').html(html);
  15. });
  16. </script>
  17. </body>
 

 

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.