views:

1166

answers:

1

Hi there, folks

Here's another one for you to help me solve: I have an ASP.NET website that uses AJAX (asynchronous) calls to am .ashx handler, passing a query string parameter to get some information from the database.

Here's an example of how it works:

Client-side (Javascript) code snippet that makes the asynchronous call to the handler:

/* Capture selected value from a DropDownBox */
var dropdown = document.getElementById(DropDownID);
var selectedValue = dropdown.options[dropdown.selectedIndex].value;

/* Make the call to the handler */
var url = "MyHandler.ashx?param=" + selectedValue;

var ajaxObj = new Ajax();
ajaxObj.doRequest(url, MyCallback, args, connectionFailed);

When I load the webform (that contains this AJAX call) for the first time, it sends the right query string to the handler (I checked it using debug in Visual Studio), like param = Street Joseph Blíss. That's the right behavior I want it to have.

The thing is that when I load that webform again (and all subsequent times), that í character from "Blíss" appears in server-side as í-. As that's the key from the entity I'm trying to select on server-side database access script, it doesn't work as it worked on 1st webform load.

I tried encoding the query string on client-side and decoding it on server-side, using something like this:

Client-side (Javascript):

var encodedParam = encodeURIComponent(selectedValue);
/* Make the call to the handler */
var url = "MyHandler.ashx?param=" + encodedParam ;

Server-side (ASP.NET, C#):

string encodedParam = context.Request.QueryString["param"];
string value = HttpUtility.UrlDecode(encodedParam, Encoding.ASCII);

...but I had no luck with it and the problem still remains. Any help?

+2  A: 

After some more searching, I found out how to solve with server-side code refinement. Here's the deal:

I had to alter my .ashx handler to parse the original parameter grabbed from the query string and convert it into UTF-8. Here's how it's made:

// original parameterized value with invalid characters
string paramQs = context.Request.QueryString["param"];
// correct parsed value from query string parameter
string param = Encoding.UTF8.GetString(Encoding.GetEncoding("iso8859-1").GetBytes(paramQs));

Happy coding, folks! :)

XpiritO
Shouldn't it be "iso-8859-1", as described here? http://msdn.microsoft.com/en-us/library/system.text.encoding.webname.aspx
Ramunas