Solve the problem of garbled Chinese characters in ajax interaction on pages encoded as gb2312
Ajax only supports utf-8 format and cannot support gb2312 encoding format, so we often encounter garbled characters when using gb2312 encoding programs using ajax. I just found a solution:
The data transmitted by the server is still gb2312 encoded, and the client uses js to convert Chinese characters into utf8 encoding and display it on the page
search.php tutorial
header("content-type: text/html; charset=gb2312");
include './search.htm';
?>search.htm
Advanced Search
Advanced Search
ajax.php
header("content-type: text/html; charset=gb2312");
$schooltype = !empty($_post['schooltype']) ? $_post['schooltype'] : 0;
switch($schooltype) {
case 0:
echo "[['40', 'Taipingxi Town Hualibao Complete Primary School'],['41', 'Taipingxi Town Changling Heilongjiang Hope Primary School'],['42', 'Letianxi Town Junior High School'], ['43', 'Letianxi Town Liantuo Junior High School']]";
break;
Case 1:
echo "[['40', 'Taipingxi Town Hualibao Complete Primary School'],['41', 'Taipingxi Town Changling Heilongjiang Hope Primary School']]";
break;
Case 2:
echo "[['42', 'Lotte Creek Town Junior High School'],['43', 'Lotte Creek Town Liantuo Junior High School']]";
break;
Default:
break;
}
?>