Ajax Chinese garbled php perfect solution (IE and Firefox compatible) _php tutorial

Source: Internet
Author: User
Recently in a project, encountered Ajax Chinese garbled problem, after an afternoon of efforts to finally perfect solution, now will write down, hope to those who still trapped around this problem and headache people have helped.


As we all know, the use of Ajax transmission and reception of Chinese parameters, if not the client and the server do the corresponding processing will appear garbled problem, in the online corresponding article also many, but some cases difficult to find in line with their ideal answer, I today is on the internet to find a lot, but almost, Talk about ASP and JSP more (I am using PHP), so in the end did not find their own satisfactory answer.


Ajax Chinese garbled can be divided into two, the first is to send a Chinese parameter to the server (Xmlhttp.open ("Get|post", Url,true)), the server received the garbled, this is also the problem I encountered today, did not do the processing before, in IE is normal , but in Firefox there is garbled, I first took the parameter output to a text, no problem found, depressed, and then I put the query in the output to observe (I am here to find out from the database and parameters related things), and finally found the problem, IE and Firefox output parameters are not the same, although the Chinese characters are the same, but there is a small difference between the connection and the front and back, so that the identification is a coding problem, in the Internet to find relevant information, have not solved the problem, but to get some inspiration, because the AJAX send data are sent by UTF-8 encoding, So to the server-side encoding conversion (I am here to use GB2312 encoding, if the use of UTF-8 should not have this step of the problem), so I on the server side of the UTF-8 to GB2312,


$str =iconv ("UTF-8", "GB2312", $str);


Then the test, in Firefox successfully solved the problem, thought the big announcement became, but then to the IE test, found that there is a problem with IE, the server side received the parameter is not value, this depressed, suddenly see the sending head set up setRequestHeader (" Content-type "," application/x-www-form-urlencoded "); Find the problem, then the parameter code is sent there:


Geturl=encodeuri (Geturl);

Geturl=encodeuri (Geturl); Two times can also be written Geturl=encodeuri (encodeURI (Geturl));

Xmlhttp.open ("GET", geturl,true);


Then go to the server side for URL decoding:


$str =urldecode ($STR); Decoding

$ str =iconv ("UTF-8", "GB2312", $ str); Encoding Conversion


Note: The decoding must precede the encoding conversion, otherwise it will not get the correct value


Save the test, IE and Firefox will be OK.


The second is the server side to the client output Chinese garbled, this kind of problem online answer more, also can solve, in order to avoid everyone again to find, I here on the copy under J


Reason: Ajax in receiving responsetext or Responsexml value is in accordance with the UTF-8 format to decode, if the server segment sent data is not UTF-8 format, It is possible that the value of receiving responsetext or responsexml is garbled.


Workaround:


Specify the format for sending data on the server:
In the JSP file:
Response.setcontenttype ("Text/text;charset=utf-8");//txt text file returned
Or
Response.setcontenttype ("Text/xml;charset=utf-8");//The XML file returned

Php:header (' content-type:text/html;charset=gb2312 ');
ASP:Response.Charset ("GB2312")
JSP:response.setHeader ("Charset", "GB2312");


Excerpt from Hurry's column

http://www.bkjia.com/PHPjc/478467.html www.bkjia.com true http://www.bkjia.com/PHPjc/478467.html techarticle recently in a project, encountered Ajax Chinese garbled problem, after an afternoon of efforts to finally perfect solution, now will write down the experience, hope for those still trapped around this problem and headache unceasingly ...

  • 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.