jQuery JSONP


JSONP Tutorial

In this chapter we will introduce you to the knowledge of JSONP.

Jsonp (JSON with Padding) is a "usage mode" of json, which allows web pages to obtain data from other domain names (websites), that is, 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.

The same-origin policy is a well-known security policy proposed by Netscape. All browsers that support JavaScript now use this policy.


##JSONP Application

1. Server-side JSONP format data

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

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

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

The server-side file jsonp.php code is:

jsonp.php file code

Example

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

Run instance»Click the "Run instance" button to view the online instance

2. The client implements the callbackFunction function

Instance

<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>

Run instance»Click the "Run instance" button to view the online instance

page display

Instance

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

Run Instance»Click the "Run Instance" button to view the online instance

Complete code of client page

Example

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<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="http://www.runoob.com/try/ajax/jsonp.php?jsoncallback=callbackFunction"></script>
</body>
</html>

Running example»Click the "Run Example" button to view the online example


##jQuery uses JSONP
The above code can use jQuery code example:

Example

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>JSONP 实例</title>
    <script src="http://cdn.static.runoob.com/libs/jquery/1.8.3/jquery.js"></script>    
</head>
<body>
<div id="divCustomers"></div>
<script>
$.getJSON("http://www.runoob.com/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>


Run Instance »

Click the "Run Instance" button to view the online instance