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怎么避免这样的语法错误

解决方案 »

  1.   

    回结果均为plain text,在Firefox错误控制台中一直报错----
    错误: 语法错误
    re:
    不是提示你返回的结果有错误吗?你设置setRequestHeader对它有用吗
      

  2.   

    是返回的结果有语法错误,但并不影响程序功能,是因为firefox默认认为他是html片段,但实际上是纯文本,因此报html语法错误。
    我在后台被请求的也没设置ContentType即可解决
      

  3.   

    但仍然困惑的是为什么用jquery请求的ajax页面不需要设置response.setContentType也没问题
      

  4.   


    同学去看一看$.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:
      

  5.   

    Accept,默认text/html
    application/text,可能不是 Firefox 接受的类型
      

  6.   


    同学去看一看$.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:The type of data that you're expecting back from the server>>
    这个是jquery拿到从服务器返回的字符串后,根据相应的datatype他帮你进行转换的,如json,它帮你把返回的内容转换为json