Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem of ajax garbled code

Shulou Source: shulou.com Published: 2022-06-03 14:23:20 09月17日 Update

This article is to share with you about how to solve the ajax garbled problem. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

After looking for some resources on the Internet, I realized that this is a coding problem. The data sent by my server is GB2312-encoded, while AJAX treats all the received data as UTF-8-encoded.

Many solutions on the Internet are based on PHP, JSP and so on on the server side, so it is relatively simple to change these coding methods. But my server side is written in C CGI program, used in embedded systems, all the output is using printf.

Finally, a header file under linux is found, which implements the conversion of various encodings. The method of use is as follows:

Int Gb2312toUtf8 (char * inbuf,char * outbuf) {# ifdef ISCHANGEiconv_t cd;int inlen,outlen;inlen=strlen (inbuf); / / gets 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-8if (cd==0) return-1 politics memset (outbuf,0,outlen); if (iconv (cd,pin,&inlen,pout,&outlen) = =-1) return-1 iconvystals close (cd); # elseStrcpy (outbuf,inbuf); # endifreturn 0;}

In this way, the content to be output is converted and then output, and there will be no garbled code.

However, the first page output is the same as the later AJAX request output, and the first output appears garbled after the conversion, so I added a macro definition and added # define ISCHANGE 1 when compiling the AJAX output program, so it can be displayed correctly.

Thank you for reading! This is the end of the article on "how to solve the ajax garbled problem". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Output coding garbled problem content server service character string data method more program first article length good practical code usage Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Microsoft OPPO Reno Docker NVidia