ajax 跨域请求数据 jsonp 示例

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).')';
?>

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