//由于浏览器的同源策略原则,所以无法跨域传值,但可以用js原理来实现 方法一:用jquery中的jsonp 请求文件: <!DOCTYPE> <html> <head> <title>lin3615</title> <script src="jquery-1.8.3.min.js" type="text/javascript"></script> <script> $(document).ready(function(){ $.ajax({ type:‘get‘, url:‘http://www.example.com/ok.php?loginuser=lin3615&loginpass=123456‘, dataType:‘jsonp‘, jsonp:‘jsonpcallback‘, jsonpCallback:‘yes‘, success: function(data) { alert("user:"+data.user+",pass:"+data.pass); }, error:function(){ alert("eeee"); } }); }); </script> </head> <body> <h1>xx</h1> </body> <html> // ok.php 文件 $arr = array( ‘user‘=>$_GET[‘loginuser‘], ‘pass‘=>$_GET[‘loginpass‘] ); echo $_GET[‘jsonpcallback‘].‘(‘.json_encode($arr).‘)‘; //方法二: <!DOCTYPE> <html> <head> <title>lin3615</title> <script> var flightHandler = function(data) { alert(data.price+","+data.tickets); } var url = "http://www.example.com/ok.php?a=code"; var script = document.createElement("script"); script.setAttribute(‘src‘, url); document.getElementsByTagName(‘head‘)[0].appendChild(script); </script> </head> <body> <h1>xx</h1> </body> <html> // ok.php 文件 if($_GET[‘a‘] == ‘codex‘) { echo ‘flightHandler({ "price":100, "tickets":22 })‘; }else { echo ‘flightHandler({ "price":1, "tickets":2 })‘; }
时间: 2024-12-17 07:42:51