且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AJAX Web应用程序中的UTF-8编码。

更新时间:2023-11-27 20:55:46

3月16日上午9:01,Allan Ebdrup < ebd ... @ noemail.noemailwrote:
On Mar 16, 9:01 am, "Allan Ebdrup" <ebd...@noemail.noemailwrote:

我有一个ajax web应用程序,我遇到了UTF-8编码的问题....

chineese chars。


我的Ajax webapplication在一个UTF-8编码的HTML页面中运行。

我从另一个HTML页面复制并粘贴一些chineese chars在IE7中查看,

也是UTF-8编码的(在google.com上搜索china)。我将

chineese字符粘贴到内容可编辑的div中。

我的Ajax webservice编译一个XML,其中来自内容的数据可编辑

div是放在CDATA部分并将其发送到服务器上的Web服务。

我使用.innerHTML读取内容可编辑div。我用以下方式使用

XMLHttpRequest来调用webservice



-----

req。 open(" POST",strUrl,true);

req.setRequestHeader(" Content-Type"," application / x-www-form-urlencoded;

charset = UTF-8");

var strSend ="&quot ;;

for(var i = 0; i< aParameters.length; i + = 2)

{

if(strSend.length!= 0)strSend + ="&&quot ;;

strSend + = aParameters [i ] +" =" + encodeURIComponent(aParameters [i + 1]);}


req.send(strSend);

-----

其中req是XMLHttpRequest对象。和aParameteres是一个数组,

contians:parameterName,parameterValue,parameterName,parameterValue,...

在发送XML之前我把它写到屏幕上这里的chineese字符正确显示




在服务器上我使用DotNet 2.0。 XML在CDATA

部分转换为SQL,该部分是针对MSSQL 2000数据库读取和执行的,其中带有chineese字符的

字符串存储在文本中当我在我的Ajax web应用程序中再次加载数据时,会调用一个web服务

,它返回CDATA部分中XML的字符串,数据,数据。


使用DataReader从数据库中读取



当显示加载的文本时,chineese字符变为

questionmarks 。


我试图将数据库中的列更改为图像,并使用

字节数组从数据库中获取数据。那是行不通的,所以我将b $ b改回来了。

我已将以下内容添加到我的web.config:

-----

< globalization

requestEncoding =" utf-8"

responseEncoding =" utf-8"

fileEncoding =" utf-8"

/>

-----

我已经将我对象上的ToXml方法更改为以下内容:

-----

//定义输出所需的编码

系统.Text.Encoding encodingOfXmlOutput = System.Text.Encoding.UTF8;

//使用(System.IO.MemoryStream memoryStream = new System创建MemoryStream来接收我们的字节

。 IO.MemoryStream())

{

//使用我们创建的memoryStream创建XmlTextWriter和

encodingOfXmlOutput

使用(System.Xml.XmlTextWriter xmlWriter = new

System.Xml.XmlTextWriter(memoryStream,encodingOfXmlOutput))

{

//设置格式选项对于Xm lTextWriter

xmlWriter.Formatting = System.Xml.Formatting.None; //输出不应该是

缩进

//写入XML

xmlWriter.WriteStartElement(" Question");

xmlWriter.WriteStartElement(" QuestionText");

xmlWriter.WriteCData(this.Text);

xmlWriter.WriteEndElement(); // QuestionText

xmlWriter.WriteEndElement(); //问题

//强制所有字节进入memoryStream

xmlWriter.Flush();

//创建缓冲区以从memoryStream接收字节

//像UTF-8这样的编码包含一个前导码(用于标识

编码的字节)

//在我们的输出中有这个序言使我们的输出无效,所以我们不会贬值那个


byte [] buffer = new byte [memoryStream.Length -

encodingOfXmlOutput.GetPreamble ().Length];

//将游标定位在memoryStream中(在序言之后)

memoryStream.Position = encodingOfXmlOutput.GetPreamble()。长度;

//将数据从memoryStream的当前位置填充到缓冲区中

memoryStream.Read(buffer,0,buffer.Length);

//返回字符串创建的Xml

返回encodingOfXmlOutput.GetString(缓冲区);}

}


-----

仍然是同样的问题。


当我将xml转换为sql时我使用以下函数:

-----

public static string Transform(XslCompiledTransform compiledTransform,

IXPathNavigable document)

{

if(compiledTransform == null)抛出新的

ArgumentNullException(" compiledTransform");

using(StringWriter writer = new StringWriter())

{

string strResult = string.Empty;

compiledTransform.Transform(document,null,writer);

strResult = writer.ToString();

返回strResult;}

}


- ---


XSLT具有以下编码

-----

<?xml version =" 1.0&QUOT; encoding =" UTF-8"?>

-----


所以我的问题如下:我的编码搞砸了哪里?我为什么不能正确保存和加载chineese chars?


任何指针都会非常感激。


我不知道其他UTF-8字符不能正常工作,但丹麦语

字符我最初有问题(???)正常工作,我想我的

解决方案适用于任何UTF-8字符。


亲切的问候,

Allan Ebdrup
I hava an ajax web application where i hvae problems with UTF-8 encoding oc
chineese chars.

My Ajax webapplication runs in a HTML page that is UTF-8 Encoded.
I copy and paste some chineese chars from another HTML page viewed in IE7,
that is also UTF-8 encoded (search for "china" on google.com). I paste the
chineese chars into a content editable div.
My Ajax webservice compiles an XML where the data from the content editable
div is placed in a CDATA section and sends it to a webservice on the server.
I read the content editable div using .innerHTML. I call the webservice
using
XMLHttpRequest in the following way:
-----
req.open("POST", strUrl, true);
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;
charset=UTF-8");
var strSend = "";
for(var i=0; i<aParameters.length; i+=2)
{
if(strSend.length!=0) strSend += "&";
strSend += aParameters[i] + "=" + encodeURIComponent(aParameters[i+1]);}

req.send(strSend);
-----
where req is the XMLHttpRequest object. and aParameteres is an array that
contians: parameterName, parameterValue, parameterName, parameterValue,...

Before I send the XML I write it to screen and here the chineese chars are
displayed correctly.

On the server i use DotNet 2.0. The XML is transformed to SQL in a CDATA
section, that is read and executed against a MSSQL 2000 database, where the
string with the chineese chars is stored in a text column.

When I load the data again in my Ajax webapplication a webservice is called
that returns the string in an XML in a CDATA section, the data is read from
the database using a DataReader.

When the text loaded is displayed the chineese chars have turned into
questionmarks.

I''ve tried to change the column in the database to a image and use a
byte-array to fetch the data from the database. that didn''t work, so I
changed it back.
I''ve added the following to my web.config:
-----
<globalization
requestEncoding="utf-8"
responseEncoding="utf-8"
fileEncoding="utf-8"
/>
-----
I''ve changed my ToXml method on my object to the following:
-----
// Define the desired encoding of the output
System.Text.Encoding encodingOfXmlOutput = System.Text.Encoding.UTF8;
// Create MemoryStream to recieve our bytes
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
{
// Create XmlTextWriter using our created memoryStream and
encodingOfXmlOutput
using (System.Xml.XmlTextWriter xmlWriter = new
System.Xml.XmlTextWriter(memoryStream, encodingOfXmlOutput))
{
// Set formatting options for XmlTextWriter
xmlWriter.Formatting = System.Xml.Formatting.None; // Output should notbe
indented
//Write XML
xmlWriter.WriteStartElement("Question");
xmlWriter.WriteStartElement("QuestionText");
xmlWriter.WriteCData(this.Text);
xmlWriter.WriteEndElement(); //QuestionText
xmlWriter.WriteEndElement(); //Question
// Force all bytes into memoryStream
xmlWriter.Flush();
// Create buffer to recieve bytes from memoryStream
// Some encodings like UTF-8 contains a preamble (bytes to identify the
encoding)
// having this preamble in our output will invalidate our output, so we wont
be grapping that.
byte[] buffer = new byte[memoryStream.Length -
encodingOfXmlOutput.GetPreamble().Length];
// Position cursor correct in memoryStream (which is after the preamble
memoryStream.Position = encodingOfXmlOutput.GetPreamble().Length;
// Fill data from current position of memoryStream into buffer
memoryStream.Read(buffer, 0, buffer.Length);
// Return string of the created Xml
return encodingOfXmlOutput.GetString(buffer);}
}

-----
Still the same problem.

When I transform the xml to sql I use the following function:
-----
public static string Transform(XslCompiledTransform compiledTransform,
IXPathNavigable document)
{
if (compiledTransform == null) throw new
ArgumentNullException("compiledTransform");
using (StringWriter writer = new StringWriter())
{
string strResult = string.Empty;
compiledTransform.Transform(document, null, writer);
strResult = writer.ToString();
return strResult;}
}

-----

The XSLT has the following encoding
-----
<?xml version="1.0" encoding="UTF-8"?>
-----

So my question is the following: Where does my encoding screw up? How come I
can''t save and load chineese chars correctly?

Any pointers would be greatly appreciated.

I don''t know what other UTF-8 chars don''t work correctly, but the danish
chars I initially had problems with (???) work correctly, I would like my
solution to work with any UTF-8 chars.

Kind Regards,
Allan Ebdrup



听起来这个问题不是你的应用程序,而是你的

数据库定义。您的网页是UTF-8,但是您的数据库

表?


假设您的数据库* IS *设置为存储UTF-8,您正在使用的查询

工具?它可能是在数据库和你的应用程序之间将多余的字符转换为?




可能你的代码很好,你应该重定向你的bug

搜索到数据库级别。


我知道这不是一个明确的答案,但我希望有所帮助。


--Sim


It sounds like the problem isn''t with your application, but with your
databse definition. Your web page is UTF-8, but is your databse
table?

Assuming that your databse *IS* set up to store UTF-8, is the query
tool you are using? It may be translating the extra characters into ?
between the database and your application.

It may be that your code is fine, and you should redirect your bug
search to the database level.

I know that''s not a definitive answer, but I hope that helps.

--Sim


Allan Ebdrup< eb **** @ noemail.noemailwrote:
Allan Ebdrup <eb****@noemail.noemailwrote:

我有一个ajax web应用程序,我在这里遇到了UTF-8编码的问题....

chineese chars。


我的Ajax webapplication在一个UTF-8编码的HTML页面中运行。

我从IE7中查看的另一个HTML页面复制并粘贴一些chineese chars,

也是UTF-8编码的(在google.com上搜索china)。我将

chineese字符粘贴到内容可编辑的div中。

我的Ajax webservice编译一个XML,其中来自内容的数据可编辑

div是放在CDATA部分并将其发送到服务器上的Web服务。

我使用.innerHTML读取内容可编辑div。我通过以下方式使用

XMLHttpRequest来呼叫网络服务


I hava an ajax web application where i hvae problems with UTF-8 encoding oc
chineese chars.

My Ajax webapplication runs in a HTML page that is UTF-8 Encoded.
I copy and paste some chineese chars from another HTML page viewed in IE7,
that is also UTF-8 encoded (search for "china" on google.com). I paste the
chineese chars into a content editable div.
My Ajax webservice compiles an XML where the data from the content editable
div is placed in a CDATA section and sends it to a webservice on the server.
I read the content editable div using .innerHTML. I call the webservice
using
XMLHttpRequest in the following way:



< snip>


请参阅 http://pobox.com /~skeet/csharp/debuggingunicode.html

-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复该团体,请不要给我发邮件

<snip>

See http://pobox.com/~skeet/csharp/debuggingunicode.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


嗨Allan,


关于此unicode transfer问题,我认为这很可能是由于SQL Server数据库中的文本转换。我在我的本地测试机器上执行了以下测试




**使用ASP.NET aspx页面来渲染< textarea并使用

客户端脚本(使用xmlhttp组件)在< textareato

服务器中发送输入,charset是utf-8,就像你做的那样


**在服务器端,我将xmlhttp发布的数据保存到文件中(也是utf-8

编码)。


基于我的测试,中文字符被正确保存。因此,您可以尝试检查服务器端的发布数据,使用调试器将代码中的
分解并检查变量或将其写入文件进行检查。如果问题是由SQL Server数据库存储引起的,我们需要对数据库表做一些进一步的研究。


如果您有任何其他发现或问题,请随时在那里。


此致,


Steven Cheng


Microsoft MSDN在线支持主管


=========================== =======================

通过电子邮件收到我的帖子通知?请参阅
http://msdn.microsoft .com / subscripti ... ult.aspx#notif

ications。


注意:MSDN托管新闻组支持服务是针对非紧急问题

如果社区或微软支持人员在1个工作日内做出初步回复是可以接受的。请注意,每个跟随

的响应可能需要大约2个工作日作为支持

专业人士与您合作可能需要进一步调查才能达到

最有效的分辨率。该产品不适用于需要紧急,实时或基于电话的交互或复杂的b $ b项目分析和转储分析问题的情况。这种性质的问题***通过联系

Microsoft客户支持服务(CSS)处理
href =http://msdn.microsoft.com/subscriptions/support/default.aspx\"target =_ blank> http://msdn.microsoft.com/subscripti...t/default.aspx


==================================== ==============


此帖子按原样提供。没有保证,也没有授予任何权利。
Hi Allan,

Regarding on this unicode transfer issue, I think it is likely due to the
text convertion in SQL Server database. I have performed the following test
on my local test machine:

** use an ASP.NET aspx page to render out a <textareaand use
client-script (with xmlhttp component) to send the input in <textareato
server, charset is utf-8 as you did

** at server-side, I save the xmlhttp posted data into a file(also utf-8
encoding).

Based on my test, the chinese characters are correctly saved. Therefore,
you can try checking the posted data at server-side, use debugger to break
into code and inspect the variable or write it into file for checking. If
the problem is caused by SQL Server database storage, we need to do some
further research against the database table.

Please feel free to pos there if you have any other finding or questions.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.