EN codeURIComponent,带U麻烦,C,I,I,G,ö [英] encodeURIComponent, trouble with ü,ç,İ,ı,ğ,ö

查看:126
本文介绍了EN codeURIComponent,带U麻烦,C,I,I,G,ö的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题。

我通过Ajax这样的方式发送值的通用处理器。

I'm sending values to Generic Handler via Ajax like that way.

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

在邮件中包含I,C,O,G,U,I,他们正在寻找这样的处理程序°,§,ö,A,那张,具有±

when message contains İ,ç,ö,ğ,ü,ı they are looking like that on Handler Ä°,ç,ö,Ä,ü,ı

我写这篇文章的AddMessage.ashx处理程序

I write this in AddMessage.ashx Handler

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

此外,我写这篇文章的母版和aspx页面

Also i write this on MasterPage and Aspx page

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

但它没有任何意义。

But its doesn't make any sense.

推荐答案

我相信,你的错误就在于编码不匹配,您的浏览器的服务器之间。如果浏览器假设你的网页是连接拉美-1 codeD(或更正确的ISO-8859-1)的EN codeURIComponent的字母ü的结果将是%u00c3%u00bc,这当跨preTED为UTF-8的服务器上会去codeD的那张。

I believe that your the error lies in an encoding mismatch between your browser your server. If the browser assumes your page is encoded in latin-1 (or more correctly iso-8859-1) the result of the encodeURIComponent for the letter 'ü' will be '%u00c3%u00bc' which when interpreted as UTF-8 on the server will be decoded as ü.

您应该不难code编码,除非你完全知道自己在做什么。尝试删除您的一些自定义编码code或全部,看看你能得到它的工作呢。

You shouldn't hardcode encodings unless you're absolutely sure of what you're doing. Try to remove some or all of your custom encoding code and see if you can get it to work then.

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

I set up a blank ASP.NET Web Application to see if I could replicate your problem.

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>

看着在调试器中去codeD的Request.QueryString [信息]产生预期的结果(U)

Looking at the decoded Request.QueryString["Message"] in the debugger produced the expected result (ü).

但是,如果我们欺骗浏览器认为该页面正​​在以ISO-8859-1发送:

But If we trick the browser into thinking that the page is being transmitted in 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 [信息]现在包含那张。而浏览器不能正确呈现在体内的AAO字符串。

The Request.QueryString["Message"] now contains "ü". And the browser can't properly render the åäö string in the body.

使用一些网络调试工具,如小提琴手或的萤火,以确定哪些编码服务器实际使用来传输内容和浏览器是什么编码相信它的接收。

Have a look using some web debugging tool like fiddler or firebug to determine what encoding the server actually uses to transmit contents and what encoding the browser believes it's receiving.

如果对消息变量的内容是从另一个AJAX请求收到你应该检查,以确保您使用的是正确的编码方式来传输这些内容也是如此。

If the contents for the 'Message' variable is received from another AJAX request you should check to make sure that you're using a proper encoding to transmit that content as well.

底线,也懒得多关于编码。没有做任何会,大部分时间,是正确的事情。

Bottom line, don't bother to much about encodings. Not doing anything will, most of the time, be the right thing to do.

这篇关于EN codeURIComponent,带U麻烦,C,I,I,G,ö的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆