Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JSONP tutorial


May 08, 2021 JSON


Table of contents


JSONP tutorial

In this section we will introduce you to JSONP.

JSON with Padding is a "usage mode" for json that allows web pages to get information from other domain names (websites), which 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 source policy.

What is a esotho-policy? It is a well-known security policy proposed by Netscape, which is now used by all JavaScript-enabled browsers.

Jsonp is implemented by using the hashtag to obtain the characteristics of different source resources for cross-domain access to a resource.


JSONP application

1. Service-side JSONP format data

If the customer wants to visit : /try/ajax/jsonp.php?jsonp?callbackFunction.

Suppose the customer expects to return JSON data: "customername1", "customername2".

The data that actually returns to the client is: callbackFunction ("customername1", "customername2").

The service-side file jsonp .php code is:

<?php header('Content-type: application/json'); 
//获取回调函数名
$jsoncallback = htmlspecialchars($_REQUEST ['jsoncallback']);
//json数据
$json_data = '["customername1","customername2"]';
//输出jsonp格式的数据
echo $jsoncallback . "(" . $json_data . ")"; ?>

2. The client implements the callbackFunction function

<script type="text/javascript">
function onCustomerLoaded(result, methodName)
{
    var html = '<ul>';
    for(var i = 0; i < result.length; i++)     {         html += '<li>' + result[i] + '</li>';
    }
    html += '</ul>';
    document.getElementById('divCustomers').innerHTML = html;
}
</script>

Page display

<div id="divCustomers"></div>

The full code for the client page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>JSONP 实例</title>
</head>
<body>
    <div id="divCustomers"></div>
    <script type="text/javascript">
        function callbackFunction(result, methodName)
        {
            var html = '<ul>';
            for(var i = 0; i < result.length; i++)             {                 html += '<li>' + result[i] + '</li>';
            }
            html += '</ul>';
            document.getElementById('divCustomers').innerHTML = html;
        }
    </script>
<script type="text/javascript" src="/try/ajax/jsonp.php?jsoncallback=callbackFunction"></script>
</body>
</html>

jQuery uses JSONP

The above code can use examples of jQuery code:

<!DOCTYPE html>
<html>
<head>
   <title>JSONP 实例</title>
   <script src="http://apps.bdimg.com/libs/jquery/1.8.3/jquery.js" rel="external nofollow" ></script> 
</head>
<body>
<div id="divCustomers"></div>
<script>
$.getJSON("/try/ajax/jsonp.php?jsoncallback=?", function(data) {
   
  var html = '<ul>';
    for(var i = 0; i < data.length; i++)     {       html += '<li>' + data[i] + '</li>';
   }
 html += '</ul>';
  
  $('#divCustomers').html(html); 
});
</script>
</body>
</html>