Debugging an AJAX you're spitting blood.

Source: Internet
Author: User
Wrote an English-Chinese dictionary, the specific process:
1. Write the local file to the memcached server in the format {English:chinese}
2. Submit English words via Ajax and return to Chinese interpretation

Problems encountered: Query the corresponding word, can be written locally through the file_put_contents function, to prove that the query to the corresponding word, on the client, through the ReadyState property can be seen in turn to return 1,2,3,4, but in the Window.alert (type RES) is undefined when displayed.

This part of the code is OK, to read and parse the local TXT format dictionary
 Fp=fopen ($fileName, ' r ') or Die (' open Ciba failed ');} Public Function Readword () {while (!feof ($this->fp)) {$word =fgets ($this->fp); $word =trim ($word); if ($word = = ") Continue, $en = $this->parseen ($word), $ch = $this->parsech ($word), $this->arr_word["$en"]= $ch;/* $this Recycle_num--;if ($this->recycle_num==0) return; */}}public function Parseen (& $word) {if (Preg_match ($this->query_en, $word, $en)) {return $en [0];} Else{echo "Match 中文版 word failed
";}} Public Function Parsech (& $word) {if (Preg_match ($this->query_ch, $word, $ch)) {return $ch [0];} Else{echo "Match Chinese failed
";}} Public Function Getword () {return $this->arr_word;} Public Function __destruct () {fclose ($this->fp);}} $word =new Word (' ciba.txt ');//$word->readword ();//echo "
";//print_r ($word->getword ());//echo"
"; */?>//This part of the code is also OK, used to write the entry memcached 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). " Character
";//exit (); foreach ($result as $en = + $ch) {$this->mem->add ($en, $ch, memcache_compressed, Time () +10*24*3600) Or Die ("Add entry Failed". __line__. "
");}} Public Function Setword ($en, $ch) {//controller determines whether the input is legal $en= $this->filterword ($en); $en = $this->mem->get ($en) or Die ( "No Entry $en"), $this->mem->set ($en, $ch, memcache_compressed, Time () +31*24*3600) or Die ("Add entry $en failed");} Public Function Getword ($en) {//controller determines whether the input is legal $en= $this->filterword ($en), $ch = $this->mem->get ($en) or Die (" Cannot find the entry $en "); return $ch;} Public Function Replaceword ($en, $ch) {//controller determines whether the input is legal $en= $this->filterword ($en); $en = $this->mem->get ($en) or Die ("Cannot find entry $en"), $this->mem->replace ($en, $ch, memcache_compressed, Time () +31*24*3600) or Die ("Replacement entry $en failed");} Public Function Deleteword ($en) {//controller determines whether the input is legal $en= $this->filterword ($en); $this->mem->delete ($en, 0) or Die ( "Delete entry $en failed");} Filter out Chinese, including the words of the space, the entry of the length greater than 20 public function Filterword ($en) {$en =trim ($en); if (Preg_match (' #[\x{4e00}-\x{9fa5},\) \.\ ( ]+ #u ', $en)) {//echo ' is temporarily unsupported for Chinese query
' If (Preg_match (' #\b[a-z]+\b#i ', $en, $res)) {if (strlen ($res [0]) >20) {//echo "character is too long
"; return Strtolower (substr ($res [0], 0,20));} return Strtolower ($res [0]);}} else if (preg_match (' #\s+# ', $en)) {//$en =explode (", $en);//echo" contains spaces
"; $res =null;if (Preg_match (' #[a-z]+ #i ', $en, $res)) {if (strlen ($res [0]) >20) {//echo" character too long
"; return Strtolower (substr ($res [0], 0,20));} return Strtolower ($res [0]);}} else if (Preg_match (' #[?_\+\?\*\^\$\#\%\&\/\\,\.! @=\ ' \ ' \ ' "" "]# ', $en, $res)) {////echo ' contains illegal characters
' If (Preg_match (' #[a-z]+ #i ', $en, $res)) {if (strlen ($res [0]) >20) {echo "character is too long
"; return Strtolower (substr ($res [0], 0,20));} return Strtolower ($res [0]);}} else if (strlen ($en) >20) {//echo "character is too long
"; 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";?>The following code is OK, according to the client-submitted English words, can successfully query the corresponding Chinese, and write to the local file successfully Getword ($en); $en = $mem->filterword ($en); $res = " ". $en." ". $ch." "; file_put_contents (' Aword.txt ', $res.") \ r \ n ", file_append);//Here is OK echo $res;//echo ' {'. $en. ': '. $res. '} ';} Else{file_put_contents (' Aword.txt ', "Receive NON data \ r \ n", file_append);}? >//I estimate the problem is in the following code, but it is to find no problem, has been shown undefined


Ajax debugging is going to spit blood.


Reply to discussion (solution)

var res=xmlhttp.responsexml;
Window.alert (typeof $res);

Is it the same? No, of course not.

var res=xmlhttp.responsexml;
Window.alert (typeof $res);

Is it the same? No, of course not.




Hey. Such an obvious mistake simply did not shine out. I wrote the code in the writing book. I can't find it. Zend Studio for eclipse running on my machine, what are the lightweight, full-featured, auto-highlighting, auto-complete IDE recommendations?

Sublime or notepad++

var res=xmlhttp.responsexml;
Window.alert (typeof $res);

Is it the same? No, of course not.



The client makes the following modifications Xmlhttp.onreadystatechange=function () {//window.alert (xmlhttp.readystate); if (xmlhttp.readystate==4 && xmlhttp.status==200) {var res=xmlhttp.responsetext;res=eval ("(" +res+) "); Window.alert (res);//var en= Res.getelementsbytagname ("en") [0].childnodes[0].nodevalue;//var ch=res.getelementsbytagname ("ch") [0].childNodes [0].nodevalue;//var en=$ ("Enword"). Value;/var ch=res.en;$ ("Chword"). innertext= en+ ": The Chinese means:" +CH;   }} The server is changed to use JSON to return data, modify the following
 
   Getword ($en); $en = $mem->filterword ($en); $res = "
 
  
  
   
    
   $en
  
   
 
   
    
   $ch
  
   
  "; file_put_contents (' Aword.txt ', $res.") \ r \ n ", file_append);//ob_start (); $res = ' {" '. $en. ': '. $ch. '} '; echo $res;} Else{file_put_contents (' Aword.txt ', "Receive NON data \ r \ n", file_append);}

Can receive data, but received an HTML page, trying to filter the results in the OB cache, but finally an empty html+ result

I don't understand. This piece of HTML is taken. *, and the result is in the back, OB filter fails

36 line should be Ob_clean ();

36 line should be Ob_clean ();



I understand why, thank you. In fact, if not Ob_clean (), in the acceptance of the use of innerHTML instead of innertext is also possible. 3Q
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.