I found some resources on the Internet to understand that this is the encoding problem, my server side sent the data is gb2312 encoding, and Ajax to receive the data are considered as UTF-8 encoding.
Many solutions on the Internet are based on PHP, JSP, and so on the server. It is easy to change these encoding methods. However, my server uses CGI compiled in C.ProgramUsed in embedded systems. All outputs use printf.
Finally, find a header file <iconv. h> in Linux, which implements conversion of various encodings. The usage is as follows:
CopyCode The Code is as follows: int gb2312toutf8 (char * inbuf, char * outbuf)
{
# Ifdef ischange
Iconv_t CD;
Int inlen, outlen;
Inlen = strlen (inbuf); // obtain the length of the input string.
Outlen = 100; // The length of the output string.
Char ** pin = & inbuf;
Char ** pout = & outbuf;
Cd = iconv_open ("UTF-8", "gb2312"); // set to gb2312 to UTF-8
If (Cd = 0) Return-1;
Memset (outbuf, 0, outlen );
If (iconv (Cd, pin, & inlen, pout, & outlen) =-1)
Return-1;
Iconv_close (CD );
# Else
Strcpy (outbuf, inbuf );
# Endif
Return 0;
}
In this way, after the content to be output is converted and then output, there will be no garbled characters.
However, the first page output and the subsequent Ajax request output are the same piece of code. After conversion, the first output is garbled, so I added a macro definition, when compiling the Ajax output program, add # define ischange 1 to display it correctly. (In fact, there should be a better solution. Please let me know. Luoliang28@126.com)