Javascript + asp xmlhttp returns garbled characters

Source: Internet
Author: User

The XMLHTTP object created using JavaScript submits a request to the ASP page, and the returned value may be garbled.

One solution is simple: Add response. charset = "gb2312" before the ASP return value ".
Complete Code As follows:
Html

<Script language = "JavaScript">
Function createxmlhttp ()
{
VaR try = {
These: function ()
{
VaR returnvalue;
For (VAR I = 0; I <arguments. length; I ++)
{
VaR Lambda = arguments [I];
Try
{
Returnvalue = Lambda ();
Break;
} Catch (e ){}
}
Return returnvalue;
}
}
VaR XMLHTTP = Try. These
(
Function () {return New activexobject ('msxml2. xmlhttp ')},
Function () {return New activexobject ('Microsoft. xmlhttp ')},
Function () {return New XMLHttpRequest ()}
) | False;
Return XMLHTTP;
}

VaR XMLHTTP = createxmlhttp ();
XMLHTTP. onreadystatechange = function (){
If (XMLHTTP. readystate = 4)
{
If (XMLHTTP. Status = 200)
{
Document. getelementbyid ("MSG"). innerhtml = XMLHTTP. responsetext;
}
}
}
XMLHTTP. Open ("Post", "responsetest. asp", true );
XMLHTTP. setRequestHeader ('content-type', 'application/X-WWW-form-urlencoded ');
XMLHTTP. Send (true );
</SCRIPT>

<Table border = "0" cellspacing = "0" cellpadding = "0" width = "500" align = "center">
<Tr align = Center Height = "8">
<TD id = "MSG"> </TD>
</Tr>
<Tr align = center>
<TD> test </TD>
</Tr>
</Table>

responsetest. ASP
<%
response. charset = "gb2312"
response. write (" result completed ")
response. end ()
%>

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.