浅论ajax跨域!从一个例子开始!
//所谓跨域,根据同源策略缘故
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <button id="button" name="button" onclick="jsonp()">button</button> <div id="div1"></div> </body> <script> function jsonp(){ var s1=document.createElement("script"); s1.src="2.php?cb=show"; document.body.appendChild(s1); } var dd=document.getElementById("div1"); function show(data){ dd.innerHTML=data.name; } </script> </html>
php文件如下,此时通过参数cb来了接的,这样就是是
<?php $show=$_GET['cb']; $xjson="{'name':'user1'}"; echo $show."(".$xjson.")"; ?>