zoukankan      html  css  js  c++  java
  • JSONP-跨域读取数据

    页面代码:

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

     客户端实现 callbackFunction 函数:

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

    服务器端代码:

    <?php
    header('Content-type: application/json');
    //获取回调函数名
    $jsoncallback = htmlspecialchars($_REQUEST ['jsoncallback']);
    //json数据
    $json_data = '["customername1","customername2"]';
    //输出jsonp格式的数据
    echo $jsoncallback . "(" . $json_data . ")";
    ?>
    sagacity_shen 战战兢兢,如履薄冰。
  • 相关阅读:
    Transformer详解
    PAT 1012
    PAT 1011
    PAT 1010
    Jordan Lecture Note-3: 梯度投影法
    PAT 1009
    PAT 1008
    Jordan Lecture Note-2: Maximal Margin Classifier
    PAT 1007
    PAT 1006
  • 原文地址:https://www.cnblogs.com/sagacity-shen/p/7597456.html
Copyright © 2011-2022 走看看