-1

这是我的 ASP 网络服务,它查询数据库并提供 Json 文件/对象:

<!--#include file="JSON_2.0.4.asp"-->
<!--#include file="JSON_UTIL_0.1.1.asp"-->
<%
Response.CodePage = 28591 
Response.CharSet = "ISO-8859-1"

'response.write("ç ã â é À á") -> characters are written correctly with or without the two lines above but not the json feed

Set Connection = Server.CreateObject("ADODB.Connection")
Set Recordset = Server.CreateObject("ADODB.Recordset")  
Connection.Open "Provider=sqloledb;SERVER=localhost;DATABASE=DB;UID=user;PWD=PASS;" 
QueryToJSON(Connection,"select field1, field2, field3, field4, FROM table").flush
%>

响应文本:

[{"col1":"value1","col2":"value1","col3":"value1"}, {"col1":"value2","col2":"value2","col3":"value2 "}, {"col1":"value3","col2":"value3","col3":"value3"}]

响应 json 未正确编码替换标点符号“\u00E9”,并在值的中间和末尾添加了大量空格。当我直接在浏览器中打开这个文件时,除了被替换的字符外,它看起来还不错,但是当我将它作为 xmlhttp.responseText 提供给 IndexedDB 时,它出现在 console.log 中,有很多空格和换行符抛出一个“ DataError:提供给操作的数据不符合要求。” 错误。我认为空格和换行符可能是由替换字符引起的,例如“\n”

更新:

我已经尝试"xhr.setRequestHeader( "Content-Type", "application/json", "Charset=ISO-8859-1");"在我的 ajax 请求中使用,但它也不起作用

4

1 回答 1

0

刚刚想通了!对数据库的查询正在检索末尾带有空格的字段,这些字段在程序运行时以某种方式递增,只需将查询行更改为:

 QueryToJSON(Connection,"select rtrim(field1) field1, rtrim(field2) field2, rtrim(field3) field3, rtrim(field4) field4 FROM table").flush

现在响应很干净并且已经接受了正确的字符集。

于 2012-09-27T14:21:17.240 回答