Javascript encodeURIComponent,与ü;有关的问题çİığö;

Javascript encodeURIComponent,与ü;有关的问题çİığö;,javascript,asp.net,ajax,Javascript,Asp.net,Ajax,我的问题是 我通过Ajax将值发送到通用处理程序,就像这样 xmlHttpReq.open("GET", "AddMessage.ashx?" + (new Date().getTime()) +"&Message=" + encodeURIComponent(Message), true); 当消息包含İ,ç,ö,ğ,ü,ı时,它们看起来就像HandlerÄ°,ç,Ã,ü,ı 我在AddMessage.ashx处理程序中编写了这个 context.Request.Con

我的问题是

我通过Ajax将值发送到通用处理程序,就像这样

xmlHttpReq.open("GET", "AddMessage.ashx?" + (new Date().getTime()) +"&Message=" + encodeURIComponent(Message), true);
当消息包含İ,ç,ö,ğ,ü,ı时,它们看起来就像HandlerÄ°,ç,Ã,ü,ı

我在AddMessage.ashx处理程序中编写了这个

    context.Request.ContentEncoding = System.Text.Encoding.UTF8;
    context.Response.ContentEncoding = System.Text.Encoding.UTF8;
我也写在母版页和Aspx页上

    Response.ContentEncoding = System.Text.Encoding.UTF8;
    Request.ContentEncoding = System.Text.Encoding.UTF8;

但是它没有任何意义。

我认为错误在于浏览器和服务器之间的编码不匹配。如果浏览器假定您的页面以拉丁语-1(或更准确地说是iso-8859-1)编码,字母“ü”的encodeURIComponent的结果将是“%u00c3%u00bc”,当在服务器上被解释为UTF-8时,将被解码为ü

你不应该硬编码,除非你绝对确定你在做什么。尝试删除部分或全部自定义编码,然后看看是否可以让它正常工作

我设置了一个空白的ASP.NET Web应用程序,看看是否可以复制您的问题

WebForm1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script>
            var client = new XMLHttpRequest();
            client.open('GET', 'Handler1.ashx?Message=' + encodeURIComponent('ü'));
            client.send();
    </script>
</head>
<body>
    åäö
</body>
</html>
Request.QueryString[“Message”]现在包含“ü”。浏览器也无法在主体中正确呈现åäö字符串

使用诸如或之类的web调试工具来确定服务器实际用于传输内容的编码以及浏览器认为它正在接收的编码

如果“Message”变量的内容是从另一个AJAX请求接收的,那么您应该检查以确保您也使用了正确的编码来传输该内容

总之,不要太在意编码。在大多数情况下,什么都不做是正确的

using System;

namespace WebApplication1 {
    public partial class WebForm1 : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            Response.ContentType = "text/html; charset=iso-8859-1";
        }
    }
}