跨域请求的原理大家都知道,本人也就不赘述了,数据传输的格式大概有XML和json等几种格式,但是相对与XML的格式来讲,他更加适用于大型数据的传输,所以本人选择使用json,来作为数据传输的格式。废话不多说,直接贴代码。。。
$url = "http://".$name."/data/text.php?callback=jQuery21405316117645496153_1494989206668&ca=archivesDomain&_=1494989206669&number=".$num; $resd = json_decode(curl_get($url));
/* * url:访问路径 */ function curl_get($url){ $testurl = $url; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $testurl); //参数为1表示传输数据,为0表示直接输出显示。 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //参数为0表示不带头文件,为1表示带头文件 curl_setopt($ch, CURLOPT_HEADER,0); $output = curl_exec($ch); curl_close($ch); return $output; }上面是PHP的跨域访问请求,使用的是curl模拟get请求,,,
$(".column").click(function () { $(".column").removeAttr("style"); $(this).attr("style", "background-color: red"); var ca = "column"; var doamin = $(this).html(); $.ajax({ async: false, url: 'http://' + doamin + '/data/text.php', // 跨域URL type: 'get', dataType: 'jsonp', jsonp: 'callback', //默认callback data: {"ca": ca}, success: function (json) { //客户端jquery预先定义好的callback函数,成功获取跨域服务器上的json数据后,会动态执行这个callback函数 if (json.msg == "ok") { var str = ""; for (var i = 0; i < json.info.length; i++) { str += "<tr>"; str += "<td><span class='getMore' son='" + json.info[i].id + "' cid='" + doamin + "'>+</span><a>"; if (json.info[i].ishidden == 1) { str += "<font color='red'>[隐]</font>"; } str += "<span class='articles' tid='" + json.info[i].id + "' eid='" + doamin + "'>" + json.info[i].typename + "</span></a>[ID:" + json.info[i].id + "]"; if (json.info[i].ispart == 0) { str += "(文档:" + json.info[i].count + ")"; } str += "</td>"; str += "<td>"; str += "<a><i class='fa fa-pencil-square-o'></i></a> "; str += "<a><i class='fa fa-trash-o delete' deid='"+json.info[i].id+"' doid='"+doamin+"'></i></a>"; str += "</td>"; str += "</tr>"; } $("tbody").html(str); } } }); });这边是jQuery的,JSONP的运行原理不清楚的可以自己百度一下,本人在这里就不赘述了。。。
以为我这边封装的东西不能用函数的形式,所以只好用判断的方式来返回数据。。。
$link = mysqli_connect( $cfg_dbhost, /* The host to connect to 连接MySQL地址 */ $cfg_dbuser, /* The user to connect as 连接MySQL用户名 */ $cfg_dbpwd, /* The password to use 连接MySQL密码 */ $cfg_dbname); /* The default database to query 连接数据库名称*/ if (!$link) { printf("Can't connect to MySQL Server. Errorcode: %s ", mysqli_connect_error()); exit; } $ca = $_GET["ca"]; $callback = isset($_GET['callback']) ? trim($_GET['callback']) : ''; $test = array(); $arra = array(); $list = array(); $array = array(); if($ca == "column"){ //获取栏目列表和栏目下文章数量 if(empty($cfg_dbprefix)){ $column = 'select * from arctype where topid = 0'; $model = mysqli_query($link, $column); while ($datas = mysqli_fetch_assoc($model)) { $array[] = $datas; } foreach ($array as $kas => $vas) { if ($res = mysqli_query($link, "select * from archives where typeid = ". $vas['id'])) { /* Fetch the results of the query 返回查询的结果 */ while ($rows = mysqli_fetch_assoc($res)) { $test[$kas][] = $rows; } } @$array[$kas]['count'] = count($test[$kas]); } $tmp['msg'] = "ok"; $tmp['info'] = $array; echo $callback."(".json_encode($tmp).")"; }else{ $column = 'select * from '.$cfg_dbprefix.'arctype where topid = 0'; $model = mysqli_query($link, $column); while ($datas = mysqli_fetch_assoc($model)) { $array[] = $datas; } foreach ($array as $kas => $vas) { if ($res = mysqli_query($link, "select * from '.$cfg_dbprefix.'archives where typeid = ". $va['id'])) { /* Fetch the results of the query 返回查询的结果 */ while ($rows = mysqli_fetch_assoc($res)) { $test[$kas][] = $rows; } } @$array[$kas]['count'] = count($test[$kas]); } $tmp['msg'] = "ok"; $tmp['info'] = $array; echo $callback."(".json_encode($tmp).")"; } }else if($ca == "columnSon"){ $did = $_GET['did']; //获取子栏目 if(empty($cfg_dbprefix)){ $colum = 'select * from arctype where reid = '.$did; $mode = mysqli_query($link, $colum); while ($dat = mysqli_fetch_assoc($mode)) { $arra[] = $dat; } foreach ($arra as $k => $v) { if ($re = mysqli_query($link, "select * from archives where typeid = ". $v['id'])) { /* Fetch the results of the query 返回查询的结果 */ while ($row = mysqli_fetch_assoc($re)) { $test[$k][] = $row; } } $arra[$k]['count'] = count($test[$k]); } if(!empty($arra)){ $tmp['msg'] = "ok"; $tmp['info'] = $arra; echo $callback."(".json_encode($tmp).")"; }else{ $tmp['msg'] = "no"; $tmp['info'] = "宝宝心情不好,不想给你数据..."; echo $callback."(".json_encode($tmp).")"; } }else{ $colum = 'select * from '.$cfg_dbprefix.'arctype where reid = '.$did; $mode = mysqli_query($link, $colum); while ($dat = mysqli_fetch_assoc($mode)) { $arra[] = $dat; } foreach ($arra as $k => $v) { if ($re = mysqli_query($link, "select * from '.$cfg_dbprefix.'archives where typeid = ". $v['id'])) { /* Fetch the results of the query 返回查询的结果 */ while ($row = mysqli_fetch_assoc($re)) { $test[$k][] = $row; } } $arra[$k]['count'] = count($test[$k]); } if(!empty($arra)){ $tmp['msg'] = "ok"; $tmp['info'] = $arra; echo $callback."(".json_encode($tmp).")"; }else{ $tmp['msg'] = "no"; $tmp['info'] = "宝宝心情不好,不想给你数据..."; echo $callback."(".json_encode($tmp).")"; } } }那个什么,callback可以自定义,jQuery会自动生成,相当于token验证,所以传回来的数据必须包含这类数据,,,,
大体就这么多,样式和结构比较简单,大家可以一起来探讨一下。。。。。。。。。。。。。。。。。。。。。。。。。。。