gpt4 book ai didi

javascript - encodeURIComponent,ü,ç,İ,ı,ğ,ö 有问题

转载 作者:行者123 更新时间:2023-11-29 22:39:55 24 4
gpt4 key购买 nike

我的问题是。

我正像那样通过 Ajax 将值发送到通用处理程序。

xmlHttpReq.open("GET", "AddMessage.ashx?" + (new Date().getTime()) +"&Message=" + encodeURIComponent(Message), true);

当消息包含 İ,ç,ö,ğ,ü,ı 它们在 Handler 上看起来像 Ä°,ç,ö,Ä,ü,ı

我在 AddMessage.ashx Handler 中写了这个

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

我也在 MasterPage 和 Aspx 页面上写这个

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

但这没有任何意义。

最佳答案

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

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

我设置了一个空白的 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"] 产生了预期的结果 (ü)。

但是如果我们欺骗浏览器认为页面是在 ISO-8859-1 中传输的:

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";
}
}
}

Request.QueryString["Message"] 现在包含“ü”。并且浏览器无法正确呈现正文中的 åäö 字符串。

使用一些 Web 调试工具看看 fiddlerfirebug以确定服务器实际使用什么编码来传输内容以及浏览器认为它正在接收什么编码。

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

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

关于javascript - encodeURIComponent,ü,ç,İ,ı,ğ,ö 有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3405001/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com