使用jquery发送ajax请求时,跟踪error错误函数内容为"parsererror";
$.ajax({type:'post',dataType:'xml',url:'http://localhost:8080/web1/test.do?opt=getPlanList&uid=123456',
error:function(XMLHttpRequest, textStatus, errorThrown){
alert(textStatus);
}
});
jquery-1.3.2.js定位3703行内容如下:
httpData: function( xhr, type, s ) {
var ct = xhr.getResponseHeader("content-type"),
xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0,
data = xml ? xhr.responseXML : xhr.responseText;
if ( xml && data.documentElement.tagName == "parsererror" )
throw "parsererror";
// Allow a pre-filtering function to sanitize the response
// s != null is checked to keep backwards compatibility
if( s && s.dataFilter )
data = s.dataFilter( data, type );
// The filter can actually parse the response
if( typeof data === "string" ){
// If the type is "script", eval it in global context
if ( type == "script" )
jQuery.globalEval( data );
// Get the JavaScript object, if JSON is used.
if ( type == "json" )
data = window["eval"]("(" + data + ")");
}
return data;
}
从以上代码跟踪发现xhr.responseXML值为空。
解决办法:
response.setCharacterEncoding("UTF-8");
response.setContentType("text/xml");