夺命雷公狗jquery---61Ajax跨域请求之方法2
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="js/jquery.js"></script> <script> //页面载入事件 $(function(){ //绑定相关的事件 $('#btnok').bind('click',function(){ //发送ajax $.get('http://www.test.com/ajax.php?fn=?',function(msg){ $(msg).each(function(im,item){ var id = item.id; var username = item.username; $('#result').append(id+'----'+username+'<hr />'); }); },'jsonp'); }); }); </script> </head> <body> <input type="button" id="btnok" value="获取数据" /> <hr /> <div id="result"></div> </body> </html>
PHP代码如下:
<?php $fn = $_GET['fn']; //链接数据库(CRM客户关系管理系统接口) mysql_connect("localhost",'root',''); mysql_query('use xsphp'); mysql_query('set names utf8'); //组装sql语句 $sql = "select * from admin"; $res = mysql_query($sql); $data = array(); while($row = mysql_fetch_assoc($res)){ $data[] = $row; } $str = json_encode($data); echo $fn."($str)"; //这里如果加上单引号就会变成字符串类型,不加单引号他就会变成OBJ类型(也就是一个数组)