views:

34

answers:

1

Hi everyone, My problem is.

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

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

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

I write this in AddMessage.ashx Handler

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

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  A: 

Hi Ümit.

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 ü.

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.

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"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
    <title></title>
    <script>
            var client = new XMLHttpRequest();
            client.open('GET', 'Handler1.ashx?Message=' + encodeURIComponent('ü'));
            client.send();
    </script>
</head>
<body>
    åäö
</body>
</html>

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

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

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.

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.

Markus Olsson