Simplifies status normalization in xhr transport. Local file test modified for clarity.

This commit is contained in:
jaubourg 2011-02-11 07:02:11 +01:00
parent 43a41ba7ec
commit 3a1d7a3c7c
2 changed files with 37 additions and 44 deletions

View file

@ -166,35 +166,14 @@ if ( jQuery.support.ajax ) {
} }
// Filter status for non standard behaviors // Filter status for non standard behaviors
status =
// IE - #1450: sometimes returns 1223 when it should be 204 // If the request is local and we have data: assume a success
if ( status === 1223 ) { // (success with no data won't get notified, that's the best we
status = 204; // can do given current implementations)
// Various - #8177: a Not Modified response was received !status && s.isLocal ?
// yet no conditional request headers was provided ( responses.text ? 200 : 404 ) :
} else if ( status === 304 && // IE - #1450: sometimes returns 1223 when it should be 204
!headers[ "if-modified-since" ] && ( status === 1223 ? 204 : status );
!headers[ "if-none-match" ] ) {
status = 200;
// Status 0 encompasses several cases
} else if ( !status ) {
// Cross-domain
if ( s.crossDomain ) {
if ( !s.statusText ) {
// FF, Webkit (other?): There is no status text for errors
// 302 is the most generic cross-domain status code
// for errors, could be anything really (even a real 0)
status = 302;
}
// All same-domain: for local files, 0 is a success
} else if( s.isLocal ) {
status = 200;
// Opera: this notifies success for all requests
// (verified in 11.01). Patch welcome.
}
// Opera - #6060: sets status as 0 for 304
// Patch welcome.
}
} }
} }
} catch( firefoxAccessException ) { } catch( firefoxAccessException ) {

View file

@ -27,20 +27,25 @@
.success { color: green; } .success { color: green; }
</style> </style>
</head> </head>
<body> <body>
<h1>jQuery Local File Test</h1> <h1>jQuery Local File Test</h1>
<h2>
Introduction
</h2>
<ul> <ul>
<li> <li>
Access this file using the "file:" protocol, Access this file using the "file:" protocol,
</li> </li>
<li> <li>
two "OK" strings must appear below, two green "OK" strings must appear below,
</li> </li>
<li> <li>
Opera will fail at detecting errors, it's a known issue. Empty local files will issue errors, it's a known limitation.
</li> </li>
</ul> </ul>
<h2>
Results
</h2>
<ul> <ul>
<li> <li>
Success: Success:
@ -53,26 +58,35 @@
</span> </span>
</li> </li>
</ul> </ul>
<h2>
Logs:
</h2>
<ul id="log">
</ul>
<script> <script>
var logUL = jQuery( "#log" );
function doLog( message, args ) {
jQuery( "<li />").appendTo( logUL ).text( message + ': "' + Array.prototype.join.call( args, '" - "' ) + '"' );
}
jQuery.ajax( "./data/badjson.js" , { jQuery.ajax( "./data/badjson.js" , {
context: jQuery( "#success" ), context: jQuery( "#success" ),
dataType: "text" dataType: "text"
}).success(function() { }).success(function( data, _, xhr ) {
console && console.log && console.log( "success/success" , arguments ); doLog( "Success (" + xhr.status + ")" , arguments );
this.addClass("success").text( "OK" ); this.addClass( data ? "success" : "error" ).text( "OK" );
}).error(function() { }).error(function( xhr ) {
console && console.log && console.log( "success/error" , arguments ); doLog( "Success (" + xhr.status + ")" , arguments );
this.addClass("error").text( "FAIL" ); this.addClass( "error" ).text( "FAIL" );
}); });
jQuery.ajax( "./data/doesnotexist.ext" , { jQuery.ajax( "./data/doesnotexist.ext" , {
context: jQuery( "#error" ), context: jQuery( "#error" ),
dataType: "text" dataType: "text"
}).error(function() { }).error(function( xhr ) {
console && console.log && console.log( "error/error" , arguments ); doLog( "Error (" + xhr.status + ")" , arguments );
this.addClass("success").text( "OK" ); this.addClass( "success" ).text( "OK" );
}).success(function() { }).success(function( data, _, xhr ) {
console && console.log && console.log( "error/success" , arguments ); doLog( "Error (" + xhr.status + ")" , arguments );
this.addClass( $.browser.opera ? "success" : "error" ).text( "FAIL" ); this.addClass( "error" ).text( "FAIL" );
}); });
</script> </script>
</body> </body>