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

ajax 跨域请求数据 jsonp 示例

时间:2015-10-13 19:10:09      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

1.请求页面

<!doctype html>
<head>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jsonp调用方法</title>
</head>
<body>
<script type="text/javascript" src="//cdn.bootcss.com/jquery/3.0.0-alpha1/jquery.min.js"></script>
<script type="text/javascript">
//访问地址http://mytest.zyq.com/form/demo.php
getdata();
function getdata(){
//方法1
jQuery.getJSON("http://10.1.13.154/form/jsonp.php?callback=?", function(data){
//alert(data)
})
//方法2
jQuery.get("http://10.1.13.154/form/jsonp.php?callback=?", function(data){
//alert(data)
},"jsonp");
//方法3
jQuery.ajax({
type:"GET",
url:"http://10.1.13.154/form/jsonp.php?callback=?",
dataType:"jsonp",
success:function(data){
alert(data)
}
}
)
}
</script>
</script>
</body>
</html>

 

2.jsonp.php 数据

<?php
$resdata = array("status"=>"1","data"=>array(),"request"=>"xxx");
echo $_GET[‘callback‘].‘(‘.json_encode($resdata,JSON_FORCE_OBJECT).‘)‘;
?>

 

ajax 跨域请求数据 jsonp 示例

标签:

原文地址:http://www.cnblogs.com/chenxingrui/p/4875336.html

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