ASP.NET和放大器;阿贾克斯:使用ISO-8859-1编码的查询字符串参数放大器、字符串、参数、ASP

2023-09-10 17:07:11 作者:你是我的命

下面是另外一个给你帮我解决:我有一个使用AJAX(异步)一个ASP.NET网页调用是 ashx的处理,传递的查询字符串的参数,从数据库中获得一些信息。

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:

客户端的(JavaScript)的的 code段的,使得给处理程序的异步调用:

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);

当我加载 Web窗体(包含此AJAX调用)的第一次,它发出了正确的查询字符串处理程序(我检查使用调试在Visual它工作室),如参数=街约瑟夫的幸福。这是正确的行为,我希望它有。

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.

的事情是,当我加载 Web窗体再(和所有后续倍),即 I 从性格幸福出现在服务器端为 A - 。因为这是从我试图在服务器端的数据库访问脚本选择实体按键,它不会因为它的工作在1日 Web窗体负荷工作。

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:

客户端的(JavaScript)的:

Client-side (Javascript):

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

服务器端的(ASP.NET,C#):

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?

推荐答案

一些搜索后,我发现了如何解决与服务器端的code细化。这里的交易:

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

我不得不改变我的ashx的处理程序来解析原始参数一把抓起的查询字符串的,并把它转换成UTF-8。下面是它是由:

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! :)