A solution to Ajax garbled _ajax related

Source: Internet
Author: User

Find some resources on the Internet to understand this is a coding problem, my server sent over the data is GB2312 encoded, and Ajax to receive the data as UTF-8 encoded.
Many of the online solutions are server-side based on PHP, JSP, and so on, to change the encoding of these methods are relatively simple. But my server side is a C-written CGI program, used in embedded systems, all the output is using printf.
Finally, we find a header file <iconv.h> under Linux, which realizes the conversion of various encodings. Use the following methods:

Copy Code code as follows:

int Gb2312toutf8 (char *inbuf,char *outbuf)
{
#ifdef Ischange
iconv_t cd;
int Inlen,outlen;
Inlen=strlen (INBUF)//Get the length of the input string
outlen=100;//output string length.
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;
}

So the content to be output after conversion and then output, will not appear garbled.
However, the first page output and the subsequent Ajax request output is the same piece of code, the first output after the conversion has garbled, so I added a macro definition, in compiling the AJAX output program, added a #define Ischange 1, so that you can correctly display. (In fact, there should be a better solution, please know the friend told me.) 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.