Simplifies status normalization in xhr transport. Local file test modified for clarity.
authorjaubourg <j@ubourg.net>
Fri, 11 Feb 2011 06:02:11 +0000 (07:02 +0100)
committerjaubourg <j@ubourg.net>
Fri, 11 Feb 2011 06:02:11 +0000 (07:02 +0100)
src/ajax/xhr.js
test/localfile.html

index f31fa70..c48ac90 100644 (file)
@@ -166,35 +166,14 @@ if ( jQuery.support.ajax ) {
                                                                        }
 
                                                                        // Filter status for non standard behaviors
-
-                                                                       // IE - #1450: sometimes returns 1223 when it should be 204
-                                                                       if ( status === 1223 ) {
-                                                                               status = 204;
-                                                                       // Various - #8177: a Not Modified response was received
-                                                                       // yet no conditional request headers was provided
-                                                                       } else if ( status === 304 &&
-                                                                                               !headers[ "if-modified-since" ] &&
-                                                                                               !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.
-                                                                       }
+                                                                       status =
+                                                                               // If the request is local and we have data: assume a success
+                                                                               // (success with no data won't get notified, that's the best we
+                                                                               // can do given current implementations)
+                                                                               !status && s.isLocal ?
+                                                                               ( responses.text ? 200 : 404 ) :
+                                                                               // IE - #1450: sometimes returns 1223 when it should be 204
+                                                                               ( status === 1223 ? 204 : status );
                                                                }
                                                        }
                                                } catch( firefoxAccessException ) {
index c27e946..96e0f98 100644 (file)
                .success { color: green; }
        </style>
 </head>
-
 <body>
        <h1>jQuery Local File Test</h1>
+       <h2>
+               Introduction
+       </h2>
        <ul>
                <li>
                        Access this file using the "file:" protocol,
                </li>
                <li>
-                       two "OK" strings must appear below,
+                       two green "OK" strings must appear below,
                </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>
        </ul>
+       <h2>
+               Results
+       </h2>
        <ul>
                <li>
                        Success:
                        </span>
                </li>
        </ul>
+       <h2>
+               Logs:
+       </h2>
+       <ul id="log">
+       </ul>
        <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" , {
                        context: jQuery( "#success" ),
                        dataType: "text"
-               }).success(function() {
-                       console && console.log && console.log( "success/success" , arguments );
-                       this.addClass("success").text( "OK" );
-               }).error(function() {
-                       console && console.log && console.log( "success/error" , arguments );
-                       this.addClass("error").text( "FAIL" );
+               }).success(function( data, _, xhr ) {
+                       doLog( "Success (" + xhr.status + ")" , arguments );
+                       this.addClass( data ? "success" : "error" ).text( "OK" );
+               }).error(function( xhr ) {
+                       doLog( "Success (" + xhr.status + ")" , arguments );
+                       this.addClass( "error" ).text( "FAIL" );
                });
                jQuery.ajax( "./data/doesnotexist.ext" , {
                        context: jQuery( "#error" ),
                        dataType: "text"
-               }).error(function() {
-                       console && console.log && console.log( "error/error" , arguments );
-                       this.addClass("success").text( "OK" );
-               }).success(function() {
-                       console && console.log && console.log( "error/success" , arguments );
-                       this.addClass( $.browser.opera ? "success" : "error" ).text( "FAIL" );
+               }).error(function( xhr ) {
+                       doLog( "Error (" + xhr.status + ")" , arguments );
+                       this.addClass( "success" ).text( "OK" );
+               }).success(function( data, _, xhr ) {
+                       doLog( "Error (" + xhr.status + ")" , arguments );
+                       this.addClass( "error" ).text( "FAIL" );
                });
        </script>
 </body>
\ No newline at end of file