Browse Source

Added jQuery approach to enable local HTTP requests with JavaScript.

Browsers usually return HTTP status 0 for requests using a local protocol like file, app...
For local development it's nice to use simulated HTTP status 200 or 404 if responseText ist available or not (jQuery does exactly that and I implemented that solution).
derRaab 11 years ago
parent
commit
d2356c80d5
1 changed files with 10 additions and 0 deletions
  1. 10 0
      std/haxe/Http.hx

+ 10 - 0
std/haxe/Http.hx

@@ -166,6 +166,16 @@ class Http {
 			if( r.readyState != 4 )
 				return;
 			var s = try r.status catch( e : Dynamic ) null;
+			if ( s != null ) {
+				// If the request is local and we have data: assume a success (jQuery approach):
+				var protocol = js.Browser.location.protocol.toLowerCase();
+				var rlocalProtocol = ~/^(?:about|app|app-storage|.+-extension|file|res|widget):$/;
+				var isLocal = rlocalProtocol.match( protocol );
+				if ( isLocal ) {
+					s = r.responseText != null ? 200 : 404;
+					
+				}
+			}
 			if( s == untyped __js__("undefined") )
 				s = null;
 			if( s != null )