Extracting UTF8 text from Ajax-based server-supplied HTML into JavaScript -


my server side code perl cgi, accessed via ajax (xmlhttprequest). application multi-lingual (english/thai).

one operation reads thai utf-8 encoded file on server side, , returns content embedded in html frame client. text responsetext element of xmlhttprequest. when display resulting string in application, in textarea or div, text shows garbled. non-thai text displays correctly.

i can invoke cgi operation directly browser , content displays correctly, problem seems related process of extracting content response , storing in javascript string variable. have tried various types of transformations during assignment, removed details post since commenters deemed them irrelevant.

the encoding in application html set correctly utf-8.


client side javascript code

function getabstract_api(projectyear,projectid,english_flag, successfunction, errorfunction) {     var requeststring = url + "?action=getabstract&projectyear=" + projectyear;     requeststring += "&projectid=" + projectid +                       "&english_flag=" + english_flag;     var request = new xmlhttprequest();     request.open("get",requeststring);     request.send(null);     request.onreadystatechange = function()        {        if (request.readystate == 4)           {                    if (request.status == 200)               {              var response = request.responsetext;              var errorpattern = /\w*error/;              if (response.match(errorpattern))                 {                 var errcode = extracterrorcode(response);                 var messagedetail = getmessagedetail(errcode);                 showi18nalert(messagedetail);                 if (errorfunction)                     errorfunction();                 return false;                 }              abstracttext = response;               if (successfunction)                 successfunction();              }           else              {              showi18nalert('requesterror_msg');              }           }        } /* end callback function */   } 

abstracttext global variable supposed hold returned text, display.


server side perl code:

sub getabstract {     $projectid = $cgi->param('projectid');     $projectyear = $cgi->param('projectyear');     $english_flag = $cgi->param('english_flag');     if (!$projectid)        {        showerror("missing required parameter 'projectid'");        }     if (!$projectyear)        {        showerror("missing required parameter 'projectyear'");        }     if (!$english_flag)        {        $english_flag = 'true';        }     # name of abstract file     $sqlcommand = "select ";     if ($english_flag eq 'true')        {        $sqlcommand .= "abstract_en_file ";        }      else        {        $sqlcommand .= "abstract_th_file ";        }     $sqlcommand .= "from projects id=$projectid;";     logentry("about execute: |$sqlcommand|\n");     $stmt = $gdbh->prepare($sqlcommand);     $numrows = $stmt->execute;     $gsqlerror= $gdbh->err;     $gsqlerrorstr = $gdbh->errstr;     if ($gsqlerror != 0)         {         showerror($gsqlerrorstr); # dies after sending error         }     if ($numrows != 1)         {         showerror("no project found id $projectid");         }     ($filename) = $stmt->fetchrow_array;     if (($english_flag eq 'false') && ($filename eq 'null'))         {         returnapplicationerror("cannot read abstract file: $filename\nerrorcode|112|\n");         }     $path = "$homedir/$gdoclocation/abstracts/$filename";     $fh;     if (!open($fh, '<',$path))        {        returnapplicationerror("cannot read abstract file: $path\nerrorcode|111|\n");        }     $filecontents = '';     @lines =  <$fh>;     foreach $line (@lines)        {        $filecontents .= "$line";            }     close $fh;     showsuccess("~abstract~\n$filecontents");     # showsuccess returns passed text within html document  } 

please don't waste time criticizing style or idioms. know there amateurish elements. i'm learning go along!

thanks.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -