jsonp示列

前端代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>test page</title>
        <script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
    </head>
    <body>
        1
    </body>
</html>
<script type="text/javascript">
    $(function() {
        $.ajax({
            url: "http://han.com/jsonp.php?callback=?",
            data: "p1=1&p2=2",
            type: "get",
            timeout: 15000,
            dataType: "jsonp",  // not "json" we'll parse
            // jsonp: "callbackd",
            success:function (data) {
                console.log(data);
            }
        })
    })
</script>

后端php代码

<?php

$callback = isset($_GET['callback']) ? $_GET['callback'] : '';

$data = [
    'page' => 1,
    'pageSize' => 10,
    'pageCount' => 100,
    'total' => 1000,
    'one' => [
        'name' => 'hanpengyu',
        'age' => 27,
        'productId' => '1001',
    ],
    'two' => [
        't1' => 'test1',
        't2' => 'test2',
    ],
];

echo $callback . "(" . json_encode($data) . ")";

  

原文地址:https://www.cnblogs.com/hanpengyu/p/6041541.html