ajax的status为201依然触发jquery的error事件的问题

2023-05-11,,

<!--
a{
color:lightblue;
text-decoration:none;
}
.cnblogs_code{
width:20%;
}
-->

昨天在调试一个ajax的时候发现,即使status是201,仍然会触发jquery的error事件。statusText是"parseerror".

通过在stackoverflow上查询,戳这里。

加上参数

dataType:"text"

就可以解决。

而问题产生的原因在评论里写的也很详细。但是并不对,他弄错了convert的位置,在jquery判断状态码前,就已经进行了ajaxConvert。

 // Determine if successful,判断status的大小
isSuccess = status >= 200 && status < 300 || status === 304; // Get response data
if (responses) {
response = ajaxHandleResponses(s, jqXHR, responses);
} // Convert no matter what (that way responseXXX fields are always set),在此处对response的data进行转化。下面附有ajaxConvert的源码。
response = ajaxConvert(s, response, jqXHR, isSuccess); // If successful, handle type chaining
if (isSuccess) { // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
if (s.ifModified) {
modified = jqXHR.getResponseHeader("Last-Modified");
if (modified) {
jQuery.lastModified[cacheURL] = modified;
}
modified = jqXHR.getResponseHeader("etag");
if (modified) {
jQuery.etag[cacheURL] = modified;
}
} // if no content
if (status === 204 || s.type === "HEAD") {
statusText = "nocontent"; // if not modified,判断状态是否为304
            } else if (status === 304) {
statusText = "notmodified"; // If we have data, let's convert it。
} else {
statusText = response.state;
success = response.data;
error = response.error;
isSuccess = !error;
}
} else {
// Extract error from statusText and normalize for non-aborts
error = statusText;
if (status || !statusText) {
statusText = "error";
if (status < 0) {
status = 0;
}
}
}
function ajaxConvert( s, response, jqXHR, isSuccess ) {
var conv2, current, conv, tmp, prev,
converters = {}, // Work with a copy of dataTypes in case we need to modify it for conversion
dataTypes = s.dataTypes.slice(); // Create converters map with lowercased keys
if ( dataTypes[ 1 ] ) {
for ( conv in s.converters ) {
converters[ conv.toLowerCase() ] = s.converters[ conv ];
}
} current = dataTypes.shift(); // Convert to each sequential dataType
while ( current ) { if ( s.responseFields[ current ] ) {
jqXHR[ s.responseFields[ current ] ] = response;
} // Apply the dataFilter if provided
if ( !prev && isSuccess && s.dataFilter ) {
response = s.dataFilter( response, s.dataType );
} prev = current;
current = dataTypes.shift(); if ( current ) { // There's only work to do if current dataType is non-auto
if ( current === "*" ) { current = prev; // Convert response if prev dataType is non-auto and differs from current
} else if ( prev !== "*" && prev !== current ) { // Seek a direct converter
conv = converters[ prev + " " + current ] || converters[ "* " + current ]; // If none found, seek a pair
if ( !conv ) {
for ( conv2 in converters ) { // If conv2 outputs current
tmp = conv2.split( " " );
if ( tmp[ 1 ] === current ) { // If prev can be converted to accepted input
conv = converters[ prev + " " + tmp[ 0 ] ] ||
converters[ "* " + tmp[ 0 ] ];
if ( conv ) { // Condense equivalence converters
if ( conv === true ) {
conv = converters[ conv2 ]; // Otherwise, insert the intermediate dataType
} else if ( converters[ conv2 ] !== true ) {
current = tmp[ 0 ];
dataTypes.unshift( tmp[ 1 ] );
}
break;
}
}
}
} // Apply converter (if not an equivalence)
if ( conv !== true ) { // Unless errors are allowed to bubble, catch and return them
if ( conv && s.throws ) {
response = conv( response );
} else {
try {
response = conv( response );
} catch ( e ) {
return {
state: "parsererror",
error: conv ? e : "No conversion from " + prev + " to " + current
};
}
}
}
}
}
} return { state: "success", data: response };
}

p.s.在ajax的同步请求情况下,onreadystatechang事件没有触发。

p.s.jquery判断了状态204(返回空),或304(用户使用缓存文档)。

注释:

· 204 - No Content 没有新文档,浏览器应该继续显示原来的文档。如果用户定期地刷新页面,而Servlet可以确定用户文档足够新,这个状态代码是很有用的。

· 304 - Not Modified 客户端有缓冲的文档并发出了一个条件性的请求(一般是提供If-Modified-Since头表示客户只想比指定日期更新的文档)。服务器告诉客户,原来缓冲的文档还可以继续使用。

ajax的status为201依然触发jquery的error事件的问题的相关教程结束。

《ajax的status为201依然触发jquery的error事件的问题.doc》

下载本文的Word格式文档,以方便收藏与打印。