jquery~~~ajax get方法实例,英汉词典,数据抓取百度字典。
----------------------------------------------------------
----------------------------------------------------------
index.php
------------------------------
<style type="text/css">
body,th,td{font-size:14px;}
</style>
<script language="javascript" src="jquery.js"></script>
<script language="javascript">
$(document).ready(function(){
$("#dicsearch").click(function(){
$.ajax({ url: "get.php",
type:"get",
dataType:"html",
data: "type=dic&zhi="+document.getElementById('dicinput').value,
success:function(msg){
$("#showdic").html(msg);
}});
});
});
</script>
<div id="dicbody">
<div id="dics" style="float:left">英汉词典:
<input name="dicinput" type="text" maxlength="20" /><input name="dicsearch" type="submit" id="dicsearch" value="查询" /></div>
<div id="showdic" style="float:left"></div>
</div>
--------------------------------------------------
get.php
--------------------------------------------------
<?
Header("Content-type: text/html;charset=GB2312");
function curl($url) {
$ch=curl_init($url);
$fp=fopen("temp.txt","a");
curl_setopt($ch,CURLOPT_FILE,$fp);
curl_setopt($ch,CURLOPT_HEADER,0);
fwrite($fp,curl_exec($ch));
curl_close($ch);
fclose($fp);
$content=file_get_contents("temp.txt");
unlink("temp.txt");
return $content;
}
$type=$_GET[type];
$zhi=$_GET[zhi];
if ($type=="dic") {
$content=curl("http://www.baidu.com/baidu?ie=gb2312&ct=1048576&cl=3&word=".$zhi);
preg_match("/(?<=<ol/>).*?(?=<//ol/>)/is",$content,$dic);
$text=str_replace("IMAGES","http://www.baidu.com/IMAGES",$dic[0]);
echo $text;
}
?>
==================================================================================
<script language="javascript" src="Scripts/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
getInfo();
// 每隔一定时间和服务器交互一次
setInterval(getInfo,10000);
});
//从服务器获取数据的方法
function getInfo(){
$.ajax({ url: "userShout.php",
type:"get",
dataType:"html",
data: "",
success:function(msg){ //回调方法 显示返回信息到 DIV:newsmsg
$("#newsmsg").html(msg);
}});
}
</script>