Chinese garbled problem when using XMLHTTP post/get HTML page

Source: Internet
Author: User
Tags abs array chr contains end html form html page
xml| Problem | page | chinese | Chinese garbled

I used to post a post with XMLHTTP post Form, where the code I post e of the value of no problem, but later found that Post contains Chinese form will appear garbled, the reason is of course, UTF-8 and GB2312 between the conversion problem! Tnnd, down gb2312!. It's good for everyone to use UTF8.

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:

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 Chinese is similar to%?? %?? 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:

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

Part ii. correct display of 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:P

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 directly from the server": the only The problem is, 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 cannot!

Code See below:

Hey, is not very simple ah, with this try again? Everything ok!

(By the way Byte (), this dongdong performance in the VBScript can only use the demon to describe-to it call VarType return 8209--vbarray + vbbyte, with LBound, UBound can get the upper bound of the array, but is not the name (i I thought I couldn't handle this type in the script, and in the BYTES2BSTR function I saw it as a string to deal with--LENB/MIDB or something, and it was an accident-- I began to pass to this function is Xmlhttp.responsetext, want a byte a byte to see inside exactly what, later on a temporary responsetext changed to Responsebody, the results of the lottery, haha)

The last of the nonsense:
1. The above code is passed in MSXML Parser 3 release+vbscript 5.5 environment. The brother has an earlier version of the script that can help me try to see if I can make it.
2, always thought JavaScript vs VBScript should be JavaScript slightly better, so sometimes want to completely discard vbscript,asp server/client all use JavaScript, it seems not a good idea.




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.