+ -
当前位置:首页 → 问答吧 → jQuery AJAX post 提交数据遇到的 parsererror 问题

jQuery AJAX post 提交数据遇到的 parsererror 问题

时间:2009-06-22

来源:互联网

大家好,初来乍到,请多关照。

是这样的,有这样一段JS代码:
复制代码
  1. function getUrl() {
  2.     return 'subOne.do?tempTime='+new Date().getTime();
  3. }
  4. function backToBack() {
  5. $.ajax({  
  6.          url: getUrl(),  
  7.          type: 'POST',
  8.          dataType: 'text',
  9.          data:"files(kakxi)="+$("#kakaxi").val()+"&mmm="+$("#mmm").val(),
  10.          error : function(XMLHttpRequest, textStatus, errorThrown) {
  11.              alert(textStatus);
  12.          },
  13.          success: function(data, textStatus){
  14.              window.opener.createXMLInput("9:00-12:00",data);
  15.          }
  16.      }
  17. );
  18. }

在FF和chrome下能够正常使用,可以得到从服务器上返回的字符串。但是到了IE下,就爆出了parsererror 错误,查了一下,看到可能是被当成XML去解析了,但是怎么改都没有解决,于是去看jQuery的源代码(我用的是1.3.2),问题定位在httpData这个方法里(3583行),这个方法的代码如下:
复制代码
  1. httpData: function( xhr, type, s ) {
  2.     
  3.     var ct = xhr.getResponseHeader("content-type"),
  4.         xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0,
  5.         data = xml ? xhr.responseXML : xhr.responseText;
  6.     if ( xml && data.documentElement.tagName == "parsererror" )
  7.         throw "parsererror";
  8.         
  9.     // Allow a pre-filtering function to sanitize the response
  10.     // s != null is checked to keep backwards compatibility
  11.     if( s && s.dataFilter )
  12.         data = s.dataFilter( data, type );
  13.     // The filter can actually parse the response
  14.     if( typeof data === "string" ){
  15.         // If the type is "script", eval it in global context
  16.         if ( type == "script" )
  17.             jQuery.globalEval( data );
  18.         // Get the JavaScript object, if JSON is used.
  19.         if ( type == "json" )
  20.             data = window["eval"]("(" + data + ")");
  21.     }
  22.     
  23.     return data;
  24. }

在IE下alert他的type,是text,于是我把这个方法暴力的改成了:
复制代码
  1. httpData: function( xhr, type, s ) {
  2.     return xhr.responseText;
  3. }

这样就报错了,还是parsererror ,我换成responseXML ,打印一个object,看不到里面的数据,伤心中,请问这个问题怎么解决?谢谢

作者: i_hzh   发布时间: 2009-06-22

解决了。。。后台应该用UTF-8的格式。。。我写的是UTF8.。。晕,我用的是google in english,然后搜出来的还是只有缓存的。。     

作者: i_hzh   发布时间: 2009-06-22

什么意思,到底怎么解决的,我现在也遇到这个问题了。

作者: sevenjoy2010   发布时间: 2009-07-29

大概就是说前台后的编码一致
php下
header('Content-Type:   text/html;   charset=utf-8')

作者: jamix   发布时间: 2009-07-30