javascript + asp xmlhttp 返回亂碼

來源:互聯網
上載者:User

 通過javascript建立的XMLHttp對象,向asp頁面提交一個請求,接收的傳回值可能會是亂碼。

有一解決方式很簡單,就是在asp傳回值前加上 response.charset="gb2312".
完成代碼如下:
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>測試 </td>
</tr>
</table>

responseTest.asp

<%
response.charset="gb2312"
Response.Write (" <font color=red> 結果完成 </font> ")
Response.End()
%>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.