<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>Document</title> <script type="text/javascript" src='./jQuery/jq.js'></script> <!--第一种方法 (原生的)--> <!-- <script> // alert($); function data(msg){ //alert(msg); for(i in msg){ alert(msg[i].title); } } </script>--> <!-- <script src="http://www.test.com/json.php?callback=data"></script>--> </head> <body> <textarea class="aaa" id="aaa" cols="30" rows="10"></textarea><button>点击</button> </body> </html> <!--附属第一章方法--> <!--<script> $('button').click(function(){ //创建script标签 var script=document.createElement('script'); script.src="http://www.test.com/json.php?callback=data"; $('head')[0].appendChild(script); }); </script>--> <!--第二种方法(ajax)--> <!--<script> $("button").click(function(){ //alert('a'); var url="http://www.test.com/json.php?callback=?"; $.get(url,function(msg){ //alert(msg); for(i in msg){ alert(msg[i].title); } },'jsonp'); }); </script> 第三种方法(getJSON)--> <script> $("button").click(function(){ var url="http://api.k780.com:88/?app=weather.future&weaid=1&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json&jsoncallback=?"; $.get(url,function(msg){ //alert(msg.result[1]['citynm']); if(msg.success==1){ //alert(msg.result[1]['days']); var str=''; str+="<center>"; str+="<h1>天气预报</h1>"; str+="<marquee direction=up scrollamount=1 height=50>"; str+="今天:<br/>"+msg.result[0]['days']+" "+msg.result[0]['week']+" "+msg.result[0]['citynm']+" "+ msg.result[0]['temperature']+" "+msg.result[1]['weather']+"<br/>"+"明天:<br/>"+msg.result[1]['days']+" "+msg.result[1]['week']+" "+msg.result[1]['citynm']+" "+ msg.result[2]['temperature']+" "+msg.result[2]['weather']+"<br/>"+"后天:<br/>"+msg.result[3]['days']+" "+msg.result[3]['week']+" "+msg.result[3]['citynm']+" "+ msg.result[3]['temperature']+" "+msg.result[3]['weather']; str+="</marquee>"; str+="</center>"; } $(".aaa").html(str); },"jsonp"); }); </script> <!--<script> $("button").click(function(){ var url="http://api.k780.com:88/?app=weather.future&weaid=1&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json&jsoncallback=?"; $.getJSON(url,function(msg){ //alert(msg); for(i in msg){ alert(msg.result[i]['citynm']); } }); }) </script>--> <!--第四种方法(file_get_contents) 利用第三个页面 调用file_get_contents()--> <!--<script type="text/javascript"> alert($); $("button").click(function(){ // alert('a'); //return false; var url="http://www.ning.com/a.php"; $.get(url,function(msg){ //alert(msg); /*for(i in msg){ alert(msg[i]['title']); }*/ for(var i=0;i<msg.length;i++){ // console.log(msg[i]['id']); alert(msg[i]['title']); } },'json'); }); </script> <!--第五种方法(头)--> <!-- 在请求的页面加上 头header('Access-Control-Allow-Origin:*'); 则不用加callback=? 安全性不高--> <!--<script> $("button").click(function(){ //alert('a'); var url="http://www.test.com/json.php"; $.get(url,function(msg){ //alert(msg); for(i in msg){ alert(msg[i].title); } },'json'); }); </script> php 页面 //header('Access-Control-Allow-Origin:*'); (安全性不高)$pdo=new PDO('mysql:host=127.0.0.1;dbname=class','root','root'); $sql="select * from news"; $result=$pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC); $str=Json_encode($result); $callback=$_GET['callback']; if($callback){ echo $callback."(".$str.")"; die; } echo $str;
php Jsonp 跨域 请求
最新推荐文章于 2023-11-09 08:00:00 发布