码迷,mamicode.com
首页 > Web开发 > 详细

jsonp跨域请求

时间:2016-08-01 19:14:15      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsonp</title>
<script type="text/javascript" src="./js/jquery-1.8.3.min.js"></script>
<!-- <script>
    var flightHandler = function(data){
        alert("你查询的航班结果是:票价" + data.price + ‘元。余票‘ + data.tickets + ‘张。‘)
    }
    var url = "http://www.plants.cn/flightResult.php?code=CA1998&callback=flightHandler";
    var script = document.createElement(‘script‘);
    script.setAttribute(‘src‘, url);
    document.getElementsByTagName(‘head‘)[0].appendChild(script);
</script> -->
<script>
$(function(){
    var url = "http://www.plants.cn/flightResult.php?code=CA1998&callback=flightHandler";
    $.ajax({
        type: "get",
        async: false,
        url: url,
        dataType: "jsonp",
        jsonp: "callback",
        success: function(data){
            alert("你查询的航班结果是:票价" + data.price + 元。余票 + data.tickets + 张。)
        }
    })
})
</script>
</head>
<body>

</body>
</html>

 flightResult.php

$code = $_GET[‘code‘];
$callback = $_GET[‘callback‘];
$result = ‘{"code":"CA1998", "price":"1780", "tickets":5}‘;
echo "$callback($result)";

 

jsonp跨域请求

标签:

原文地址:http://www.cnblogs.com/tianxintian22/p/5726799.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!