Firefox中XMLHttpRequest返回结果在错误控制台报--语法错误

askquestion_007 2013-08-11 12:36:18
test.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>test</title>
<script type="text/javascript">
function test(){
var xmlHttp=null;
if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}else if(window.ActiveXObject){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var url="called.html";
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
alert(xmlHttp.responseText);
}
};
xmlHttp.open("GET",url,true);
xmlHttp.setRequestHeader("Content-Type", "application/text");
xmlHttp.setRequestHeader("Accept","application/text");
xmlHttp.send(null);
}
</script>
</head>
<body onload="test()">
</body>
</html>

called.html

result

我经常有这样的请求,返回结果均为plain text,在Firefox错误控制台中一直报错----
错误: 语法错误
result

请问我设置了xmlHttp.setRequestHeader("Content-Type", "application/text");
xmlHttp.setRequestHeader("Accept","application/text");
怎么都无效?
为何用jquery不会发生这样的语法错误?
原生xmlHttp怎么避免这样的语法错误
...全文
330 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
街头小贩 2013-08-11
  • 打赏
  • 举报
回复
回结果均为plain text,在Firefox错误控制台中一直报错---- 错误: 语法错误 re: 不是提示你返回的结果有错误吗?你设置setRequestHeader对它有用吗
街头小贩 2013-08-11
  • 打赏
  • 举报
回复
不明白request是什么意思?
askquestion_007 2013-08-11
  • 打赏
  • 举报
回复
引用 5 楼 xiaofanku 的回复:
[quote=引用 4 楼 askquestion_007 的回复:] 但仍然困惑的是为什么用jquery请求的ajax页面不需要设置response.setContentType也没问题
同学去看一看$.ajax的datatype的默认值是什么 The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). The available types (and the result passed as the first argument to your success callback) are:[/quote] The type of data that you're expecting back from the server>> 这个是jquery拿到从服务器返回的字符串后,根据相应的datatype他帮你进行转换的,如json,它帮你把返回的内容转换为json
fzfei2 2013-08-11
  • 打赏
  • 举报
回复
Accept,默认text/html application/text,可能不是 Firefox 接受的类型
街头小贩 2013-08-11
  • 打赏
  • 举报
回复
引用 4 楼 askquestion_007 的回复:
但仍然困惑的是为什么用jquery请求的ajax页面不需要设置response.setContentType也没问题
同学去看一看$.ajax的datatype的默认值是什么 The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). The available types (and the result passed as the first argument to your success callback) are:
askquestion_007 2013-08-11
  • 打赏
  • 举报
回复
但仍然困惑的是为什么用jquery请求的ajax页面不需要设置response.setContentType也没问题
askquestion_007 2013-08-11
  • 打赏
  • 举报
回复
引用 1 楼 xiaofanku 的回复:
不明白request是什么意思?
是返回的结果有语法错误,但并不影响程序功能,是因为firefox默认认为他是html片段,但实际上是纯文本,因此报html语法错误。 我在后台被请求的也没设置ContentType即可解决

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧