A solution for Ajax garbled characters

Source: Internet
Author: User

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)

Related Article

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.