Debugging an ajax job is about to vomit

Source: Internet
Author: User
When debugging an ajax file, I wrote an English-Chinese Dictionary. the specific process is as follows: 1. & nbsp; write the local file in the format of {English: & nbsp; Chinese} to the memcached server 2. & nbsp; submit an English word through ajax and return the problem encountered in Chinese interpretation: & nbsp; query the corresponding word. you can use the file_put_contents function to write it locally to prove that the corresponding word is queried, on the client, debug an ajax file through re to vomit blood.
I wrote an English-Chinese Dictionary. the specific process is as follows:
1. Write Local files to the memcached server in the format of {English: Chinese}
2. submit English words through ajax and return Chinese meaning

Problem: to query the corresponding word, you can use the file_put_contents function to write it locally to prove that the corresponding word is queried. on the client side, the readyState attribute shows that 1, 2, 3, 4 are returned in sequence, but in window. alert (type res) is displayed as undefined.



// This part of the code is OK, used to read and parse the local txt format Dictionary
Class Word
{
Private $ query_en = '# \ w + \ B # I ';
Private $ query_ch = '# [\ x {4e00}-\ x {9fa5}] [\ x {4e00}-\ x {9fa5 },\)\. \ (\ w] * # U ';
Private $ arr_word = array ();
Private $ recycle_num= 100;
Private $ fp = null;

Public function _ construct ($ fileName)
{
$ This-> fp = fopen ($ fileName, 'r') or die ('failed to open ciba ');
}


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 english 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 into 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). "character
";
// Exit ();
Foreach ($ result as $ en => $ ch)
{
$ This-> mem-> add ($ en, $ ch, MEMCACHE_COMPRESSED, time () + 10*24*3600) or die ("An error occurred while adding the entry ". _ LINE __."
");
}
}

Public function setWord ($ en, $ ch)
{
// The controller determines whether the input is valid.
$ En = $ this-> filterWord ($ en );
$ En = $ this-> mem-> get ($ en) or die ("No entry found $ en ");
$ This-> mem-> set ($ en, $ ch, MEMCACHE_COMPRESSED, time () + 31*24*3600) or die ("failed to add entry $ en ");

}

Public function getWord ($ en)
{
// The controller determines whether the input is valid.
$ En = $ this-> filterWord ($ en );
$ Ch = $ this-> mem-> get ($ en) or die ("No entry found $ en ");
Return $ ch;
}

Public function replaceWord ($ en, $ ch)
{
// The controller determines whether the input is valid.
$ En = $ this-> filterWord ($ en );
$ En = $ this-> mem-> get ($ en) or die ("No entry found $ en ");
$ This-> mem-> replace ($ en, $ ch, MEMCACHE_COMPRESSED, time () + 31*24*3600) or die ("replacing entry $ en failed ");
}

Public function deleteWord ($ en)
{
// The controller determines whether the input is valid.
$ En = $ this-> filterWord ($ en );
$ This-> mem-> delete ($ en, 0) or die ("An error occurred while deleting the entry $ en ");
}

// Filter out Chinese characters, including space phrases and entries with a length greater than 20
Public function filterWord ($ en)
{
$ En = trim ($ en );


If (preg_match ('# [\ x {4e00}-\ x {9fa5}, \) \. \ (] + # U', $ en ))
{
// Echo 'Chinese query is not supported currently
';
If (preg_match ('# \ B [a-z] + \ B # I', $ en, $ res ))
{
If (strlen ($ res [0])> 20)
{
// Echo "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 "is too long
";
Return strtolower (substr ($ res [0], 0, 20 ));
}
Return strtolower ($ res [0]);
}
}
Else if (preg_match ('# [-_ \ + \? \ * \ ^ \ $ \ # \ % \&\/\\,\.! @ = \ '\ "\"] #', $ En, $ res ))
{
//
// Echo 'contains invalid characters
';
If (preg_match ('# [a-z] + # I', $ en, $ res ))
{
If (strlen ($ res [0])> 20)
{
Echo "is too long
";
Return strtolower (substr ($ res [0], 0, 20 ));
}
Return strtolower ($ res [0]);
}
}

Else if (strlen ($ en)> 20)
{
// Echo "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 also OK. based on the English words submitted by the client, you can query the corresponding Chinese characters and write the local files successfully.
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", 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); // here is OK
Echo $ res;
// Echo '{'. $ en. ':'. $ res .'}';
}
Else
{
File_put_contents('aword.txt ', "receive NON data \ r \ n", FILE_APPEND );
}

?>

// I guess the problem lies in the following code, but the undefined is always displayed.





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.