asp输出xml格式文件时的一个诡异错误及解决方法:XML declaration allowed only at the start of the document
2017-10-28 14:18
931 查看
今天要做asp根据输入参数来判断输出文本的格式,做到输出xml时发生了一个很诡异的错误,但是输出的数据又是正确的,看着不爽,相关资料也查不到,自己想办法解决了。
先来看看这个很诡异的错误:
代码片段如下:
这里。一开始以为是
需要放在第一行,试了,无用,依旧报错。后来将
放到第一行问题解决。但是这不符合我的要求。于是试了将
改为用代码输出,居然就可以了。
可见,估计是输出xml时检测整个文本,因为还有好多语句跟着前后,故报错。而改为直接输出后,浏览器得到的就只是单一的xml文本数据再进行格式判断,这样就正常了。
以上部分将原本的直接写xml头信息改为输出:
附上asp的 response.contentType 备查:
‘-------以下内容摘录自知道
ajax开发中在请求服务器端的响应时, 对于每一种返回类型 规范的做法是要在服务端指定response的contentType
常遇到下面的几种情况:
1、 服务端需要返回一段普通文本给客户端,Content-Type="text/plain"
2 、服务端需要返回一段HTML代码给客户端 ,Content-Type="text/html"
3 、服务端需要返回一段XML代码给客户端 ,Content-Type="text/xml"
4 、服务端需要返回一段javascript代码给客户端
5 、服务端需要返回一段json串给客户端
我们主要讨论返回javascript代码和Json对象的情况。
javascript 的 contentType 按最标准的写法 应该是 application/javascript。而常用的
text/javascript 已经被 rfc定义为废弃的。
但是 在这里暂时不建议使用 application/javascript . 大家还是继续使用 text/javascript
为好. 因为很多老旧浏览器并不支持 application/javascript .
而所有浏览器都支持text/javascript. 在标准和广泛的兼容性之间 还是暂且选择后者吧。
json 的 contentType 常见写法有 : text/json &
text/javascript .
但是 这个 text/json 其实是根本不存在的, 而 text/javascript 在有些时候客户端处理起来会有歧义.
对于json的contentType , rfc里定义的标准写法是 :application/json.
在这里毫无疑问 我们应该选择标准写法的 application/Json。
’-------------------
先来看看这个很诡异的错误:
代码片段如下:
<%@ CODEPAGE=936 %> <% Option Explicit %> <% Response.Charset="gb2312" %> <!--#include file="getdata.asp"--> <% loadtype=Request("t") select case loadtype case "" case "json" case "xml" %> <% response.contentType="text/xml" %> <?xml version='1.0' encoding='gb2312'?> <a> <b> <c><%= site(1,1)%></c> <d><%=Plutname(2)%></d> <e><%=Plut(2,1)%></e> </b> </a> <% case "m" if errmsg="" then else Response.write "<span>" & errmsg & "</span>" end if %> </body> </html> <% case "w" %> <!DOCTYPE html> <html> <head> </head> <BODY>此处略</body> </html> <% end select %>以上错误出现在
<?xml version='1.0' encoding='gb2312'?>
这里。一开始以为是
<% response.contentType="text/xml" %>
需要放在第一行,试了,无用,依旧报错。后来将
<?xml version='1.0' encoding='gb2312'?>
放到第一行问题解决。但是这不符合我的要求。于是试了将
<?xml version='1.0' encoding='gb2312'?>
改为用代码输出,居然就可以了。
可见,估计是输出xml时检测整个文本,因为还有好多语句跟着前后,故报错。而改为直接输出后,浏览器得到的就只是单一的xml文本数据再进行格式判断,这样就正常了。
case "xml" %> <% response.contentType="text/xml" %> <% Response.write "<?xml version='1.0' encoding='gb2312'?>" %> <a> <b> <c><%= site(1,1)%></c> <d><%=Plutname(2)%></d> <e><%=Plut(2,1)%></e> </b> </a> <%
以上部分将原本的直接写xml头信息改为输出:
<% Response.write "<?xml version='1.0' encoding='gb2312'?>" %>这样就正常了。
附上asp的 response.contentType 备查:
‘-------以下内容摘录自知道
ajax开发中在请求服务器端的响应时, 对于每一种返回类型 规范的做法是要在服务端指定response的contentType
常遇到下面的几种情况:
1、 服务端需要返回一段普通文本给客户端,Content-Type="text/plain"
2 、服务端需要返回一段HTML代码给客户端 ,Content-Type="text/html"
3 、服务端需要返回一段XML代码给客户端 ,Content-Type="text/xml"
4 、服务端需要返回一段javascript代码给客户端
5 、服务端需要返回一段json串给客户端
我们主要讨论返回javascript代码和Json对象的情况。
javascript 的 contentType 按最标准的写法 应该是 application/javascript。而常用的
text/javascript 已经被 rfc定义为废弃的。
但是 在这里暂时不建议使用 application/javascript . 大家还是继续使用 text/javascript
为好. 因为很多老旧浏览器并不支持 application/javascript .
而所有浏览器都支持text/javascript. 在标准和广泛的兼容性之间 还是暂且选择后者吧。
json 的 contentType 常见写法有 : text/json &
text/javascript .
但是 这个 text/json 其实是根本不存在的, 而 text/javascript 在有些时候客户端处理起来会有歧义.
对于json的contentType , rfc里定义的标准写法是 :application/json.
在这里毫无疑问 我们应该选择标准写法的 application/Json。
’-------------------
相关文章推荐
- XML declaration allowed only at the start of the document 问题解决方法
- php 输出xml错误不能解析,XML declaration allowed only at the start of the document
- Php输出Xml报错:XML declaration allowed only at the start of the document
- error on line 1 at column 6: XML declaration allowed only at the start of the document
- simplexml_load_string报错:XML declaration allowed only at the start of the document
- delphi 7 调用web services时报 Invalid at the top level of the document错误的解决方法
- XML文件要有根标签(Extra content at the end of the document in file 错误)
- 【XML码农】【Error】The XML declaration may only appear at the very beginning of the document.
- Web.xml文件提示No grammar constraints (DTD or XML Schema) referenced in the document. 警告的解决方法
- Spring如何加载XSD文件(org.xml.sax.SAXParseException: Failed to read schema document错误的解决方法)
- Spring如何加载XSD文件(org.xml.sax.SAXParseException: Failed to read schema document错误的解决方法)
- xcode4.3发布报错 At least one of the following architecture(s) must be present: armv6 (-19033) 错误解决方法
- Spring如何加载XSD文件(org.xml.sax.SAXParseException: Failed to read schema document错误的解决方法)
- Spring如何加载XSD文件(org.xml.sax.SAXParseException: Failed to read schema document错误的解决方法)
- 读取xml文件出现错误的一个解决方法(android)
- 关于xml配置文件无元素提示和the root element of the document is not <xsd:schema>.错误
- Spring如何加载XSD文件(org.xml.sax.SAXParseException: Failed to read schema document错误的解决方法)
- 错误“a label can only be part of a statement and a declaration is not a statement”解决方法
- Spring如何加载XSD文件(org.xml.sax.SAXParseException: Failed to read schema document错误的解决方法)
- Spring如何加载XSD文件(org.xml.sax.SAXParseException: Failed to read schema document错误的解决方法)