瀏覽代碼

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 年之前
父節點
當前提交
d2356c80d5
共有 1 個文件被更改,包括 10 次插入0 次删除
  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 )