Solution of XMLHTTP garbled in Ajax (utf8,gb2312 encoding decoding)

Source: Internet
Author: User
Tags abs chr copy end html form
ajax|xml| Code | solve

When using XMLHTTP Post form, there are two reasons for the--post of the form when the form data is garbled, and the server response is XMLHTTP incorrectly encoded. In other words, this article mainly solves two problems-how to post Chinese content correctly & how to correctly display the Chinese content.

Part I:post Chinese content

First look at how the form E is submitted:

Code:[copy to Clipboard]

If the Stra = "SUBMIT1=SUBMIT&TEXT1=SCSDFSD", replace it with:

Stra = "submit1=submit&text1= Chinese";

You will find that the things submitted to the wrong, ASP Request.Form ("Text1") can not get the value. I use Request.BinaryRead to write a post in an HTML form to look at, only to find the problem--form submitted to the code, after the encoding of the Chinese is class in%?? %?? Escape characters, such as "Chinese", are encoded as:%D6%D0%CE%C4. Oh, also blame me stupid, someone else content-type clearly write clearly--application/x-www-form-urlencoded,urlencoded well, of course it is this appearance. In this way, we also know what to do-to do the conversion, the code see below:

Code:[copy to Clipboard]

(Here I changed the previous JavaScript code to VBScript, not full of nothing to do, after the reason see)

Part Ii.: Correct display of the Chinese content

OK, if you write the form content to the database/file on the server side, you will see no problem in Chinese there, but if you want to see the response--problem with the server: If the response result is not XML, Xmlhttp.responsexml of course there will be no dongdong, then use ResponseText good, at the end of the code add a sentence:

Alert (Oreq.responsetext)

Look at the results of our hard work.

But ... but. How all the Chinese have become squares?

I can't get it out, I'm interested in trying it myself, and I don't have to post,get a Web page containing Chinese to find it. )

The reason is simple: when XMLHTTP gets response, it assumes that response is UTF8 encoded, and if response is XML, you can specify the encoding by encoding, but HTML is not. (Damn GB2312, knock it down again!) So it has GB2312 encoded HTML as a UTF8 format, not a mistake to have a ghost!

But fortunately there is a remedy: XMLHTTP's Responsebody attribute contains the resonse--"a raw undecoded bytes as received from the server", the only problem Yes, Responsebody returns a unsigned bytes array, how do we access it, how do we convert it to BSTR?

That's why I changed the code to VBScript on top--vbscript Can do it,but JavaScript

Code See below:

Code:[copy to clipboard]

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.