themes/base: fix error r7144, location.host already includes the port number and...
[project/luci.git] / themes / base / htdocs / luci-static / resources / xhr.js
index 7e2e3b0..23bf96e 100644 (file)
@@ -43,8 +43,7 @@ XHR = function()
                var xhr  = this._xmlHttp;
                var code = this._encode( data );
 
                var xhr  = this._xmlHttp;
                var code = this._encode( data );
 
-               url = 'http://' + location.hostname +
-                       ( location.port ? ':' + location.port : '' ) + url;
+               url = location.protocol + '//' + location.host + url;
 
                if( code )
                        if( url.substr(url.length-1,1) == '&' )
 
                if( code )
                        if( url.substr(url.length-1,1) == '&' )
@@ -57,7 +56,17 @@ XHR = function()
                xhr.onreadystatechange = function()
                {
                        if( xhr.readyState == 4 ) {
                xhr.onreadystatechange = function()
                {
                        if( xhr.readyState == 4 ) {
-                               callback( xhr );
+                               var json = null;
+                               if( xhr.getResponseHeader("Content-Type") == "application/json" ) {
+                                       try {
+                                               json = eval('(' + xhr.responseText + ')');
+                                       }
+                                       catch(e) {
+                                               json = null;
+                                       }
+                               }
+
+                               callback( xhr, json );
                        }
                }
 
                        }
                }
 
@@ -134,6 +143,9 @@ XHR = function()
 
        this._encode = function(obj)
        {
 
        this._encode = function(obj)
        {
+               obj = obj ? obj : { };
+               obj['_'] = Math.random();
+
                if( typeof obj == 'object' )
                {
                        var code = '';
                if( typeof obj == 'object' )
                {
                        var code = '';
@@ -142,6 +154,7 @@ XHR = function()
                        for( var k in obj )
                                code += ( code ? '&' : '' ) +
                                        k + '=' + encodeURIComponent( obj[k] );
                        for( var k in obj )
                                code += ( code ? '&' : '' ) +
                                        k + '=' + encodeURIComponent( obj[k] );
+
                        return code;
                }
 
                        return code;
                }