日期:2014-05-17  浏览次数:20638 次

JS传汉字到ashx变成乱码了
js里alert还是汉字,传到ashx变成乱码,在网上查说加那个contentType什么的utf-8,可没说在哪加怎么加?试了好多还是乱码。。。
function getFileName() {
            var path = document.getElementById("Main_FileUpload1").value;
            var pos1 = path.lastIndexOf("\\");
            var pos2 = path.lastIndexOf(".");

            var pos = path.substring(pos1 + 1, pos2);
            alert(pos);

            var xhr = createXhr();
            xhr.open("get", "AjaxTest.ashx?name=" + pos, true);
            xhr.setRequestHeader("contentType", "application/x-www-form-urlencoded; charset=UTF-8");
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4) {
                    if (xhr.responseText == "y") {
                        alert(xhr.responseText);
                    } else {
                        alert(xhr.responseText);
                    }
                }
            }
            xhr.send(null);
        }


ASHX
<%@ WebHandler Language="C#" Class="AjaxTest" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Haiyang.DAL;
using System.IO;