调试一个ajax要吐血了 写了一个英汉词典,具体流程: 1. 把本地文件按照{English: Chinese}的格式写入memcached服务器 2. 通过ajax提交英语单词,并返回中文释义 遇到的问题: 查询对应的单词,可以通过file_put_contents函数写入本地,证明查询到了相应的单词,在客户端,通过readyState属性可以依次看到返回1,2,3,4,但是在window.alert(type res)时显示未定义。
<br /><br />//这部分代码是OK的,用于读取并解析本地的txt格式词典<br /><?php <br />class Word<br />{<br /> private $query_en='#\w+\b#i';<br /> private $query_ch='#[\x{4e00}-\x{9fa5}][\x{4e00}-\x{9fa5},\)\.\( \w]*#u';<br /> private $arr_word=array();<br /> private $recycle_num=100;<br /> private $fp=null;<br /> <br /> public function __construct($fileName)<br /> {<br /> $this->fp=fopen($fileName,'r') or die('打开ciba失败');<br /> }<br /> <br /> <br /> public function readWord()<br /> {<br /> while(!feof($this->fp))<br /> {<br /> $word=fgets($this->fp);<br /> $word=trim($word);<br /> if($word=='') continue;<br /> <br /> $en=$this->parseEn($word);<br /> $ch=$this->parseCh($word);<br /> $this->arr_word["$en"]=$ch;<br /> <br /> /* $this->recycle_num--;<br /> if($this->recycle_num==0) return; */<br /> <br /> <br /> }<br /> }<br /> public function parseEn(&$word)<br /> {<br /> if(preg_match($this->query_en, $word, $en))<br /> {<br /> return $en[0];<br /> }<br /> else<br /> {<br /> echo "match english word failed<br />";<br /> }<br /> }<br /> public function parseCh(&$word)<br /> {<br /> if(preg_match($this->query_ch, $word, $ch))<br /> {<br /> return $ch[0];<br /> }<br /> else<br /> {<br /> echo "match chinese failed<br />";<br /> }<br /> }<br /> <br /> public function getWord()<br /> {<br /> return $this->arr_word;<br /> }<br /> <br /> public function __destruct()<br /> {<br /> fclose($this->fp);<br /> }<br />}<br /><br />//$word=new Word('ciba.txt');<br />//$word->readWord();<br />//echo "<pre class="brush:php;toolbar:false">";<br />//print_r($word->getWord());<br />//echo " 登入後複製
"; */
?>
//这部分代码也是OK的,用于将词条写入memcached
require_once('parseWord.php');
class MemStore
{
private $mem=null;
private $pat='#^[a-zA-Z]+\b#i';
public function __construct()
{
$this->mem=new Memcache();
$this->mem->connect("127.0.0.1", 11211) or die("connect memcached failed!!!
");
}
public function __destruct()
{
$this->mem->close();
}
public function addWord()
{
$word=new Word('ciba.txt');
$word->readWord();
$result=$word->getWord();
//echo count($result)."字符
";
//exit();
foreach($result as $en => $ch)
{
$this->mem->add($en, $ch, MEMCACHE_COMPRESSED, time()+10*24*3600) or die("添加词条失败". __LINE__ ."
");
}
}
public function setWord($en,$ch)
{
//控制器判断输入是否合法
$en=$this->filterWord($en);
$en=$this->mem->get($en) or die("找不到词条 $en");
$this->mem->set($en, $ch, MEMCACHE_COMPRESSED, time()+31*24*3600) or die("添加词条$en失败");
}
public function getWord($en)
{
//控制器判断输入是否合法
$en=$this->filterWord($en);
$ch=$this->mem->get($en) or die("找不到词条 $en");
return $ch;
}
public function replaceWord($en,$ch)
{
//控制器判断输入是否合法
$en=$this->filterWord($en);
$en=$this->mem->get($en) or die("找不到词条 $en");
$this->mem->replace($en, $ch, MEMCACHE_COMPRESSED, time()+31*24*3600) or die("替换词条$en失败");
}
public function deleteWord($en)
{
//控制器判断输入是否合法
$en=$this->filterWord($en);
$this->mem->delete($en,0) or die("删除词条$en失败");
}
//过滤掉中文,包括空格的词组,长度大于20的词条
public function filterWord($en)
{
$en=trim($en);
if(preg_match('#[\x{4e00}-\x{9fa5},\)\.\(]+#u', $en))
{
//echo '暂时不支持中文查询
';
if(preg_match('#\b[a-z]+\b#i', $en, $res))
{
if(strlen($res[0])>20)
{
//echo "字符过长
";
return strtolower(substr($res[0], 0,20));
}
return strtolower($res[0]);
}
}
else if(preg_match('#\s+#', $en))
{
//$en=explode(' ', $en);
//echo "含有空格
";
$res=null;
if(preg_match('#[a-z]+#i', $en, $res))
{
if(strlen($res[0])>20)
{
//echo "字符过长
";
return strtolower(substr($res[0], 0,20));
}
return strtolower($res[0]);
}
}
else if(preg_match('#[—_\+\?\*\^\$\#\%\&\/\\,\.!@=\`\'\"\"""]#',$en, $res))
{
//
//echo '含有非法字符
';
if(preg_match('#[a-z]+#i', $en, $res))
{
if(strlen($res[0])>20)
{
echo "字符过长
";
return strtolower(substr($res[0], 0,20));
}
return strtolower($res[0]);
}
}
else if(strlen($en)>20)
{
//echo "字符过长
";
return strtolower(substr($en, 0,20));
}
else
{
return $en;
}
}
public function flushAll()
{
$this->mem->flush();
}
public function getTime()
{
if (function_exists("micro_time"))
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
else
{
return time();
}
}
}
//$mem=new MemStore();
//$mem->addWord();
//$mem->flushAll();
//$mem->replaceWord('abandon', 100000000);
//$mem->deleteWord('abandon');
//echo $mem->getWord('_*&^%abandon^%$#');
//echo "ok";
?>
//下面这段代码也是OK的,根据客户端提交的英语单词,可以成功查询到对应的中文,并写入本地文件成功过
header("content-type: text/xml; charset=utf-8");
header("Cache-Control: no-cache");
require_once "storeWord.php";
if(!empty($_GET['enword']))
{
$en=$_GET['enword'];
//file_put_contents('aword.txt', $en."\t\t",FILE_APPEND);
$mem=new MemStore();
$ch=$mem->getWord($en);
$en=$mem->filterWord($en);
$res="
".$en." ".$ch." ";
file_put_contents('aword.txt', $res."\r\n",FILE_APPEND);//这里是OK的
echo $res;
//echo '{'.$en.':'.$res.'}';
}
else
{
file_put_contents('aword.txt', "receive NON data \r\n",FILE_APPEND);
}
?>
//我估计问题出在下面这段代码,,但是就是找不出问题所在,一直显示undefined
ajax调试要吐血了
------解决思路---------------------- var
res =xmlhttp.responseXML;
window.alert(typeof
$res );
一样吗?不一样当然不行
------解决思路---------------------- sublime or notepad++
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
2024-10-22 09:46:29
2024-10-13 13:53:41
2024-10-12 12:15:51
2024-10-11 22:47:31
2024-10-11 19:36:51
2024-10-11 15:50:41
2024-10-11 15:07:41
2024-10-11 14:21:21
2024-10-11 12:59:11
2024-10-11 12:17:31