[javascript] fix loadRemote implementation for synchronous requests
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Wed, 20 Oct 2010 09:03:37 +0200
changeset 6555 f8b36b1e98bf
parent 6554 d60250252fc6
child 6557 37b33760bc4a
[javascript] fix loadRemote implementation for synchronous requests In case of synhronous requests, loadRemote is supposed to return the "interpreted" server result (e.g a json object, a DOM object, ...). $.ajax() always return the XHR object, so we have to use the success callback to capture the actual server result and return it later.
web/data/cubicweb.ajax.js
--- a/web/data/cubicweb.ajax.js	Tue Oct 19 17:57:56 2010 +0200
+++ b/web/data/cubicweb.ajax.js	Wed Oct 20 09:03:37 2010 +0200
@@ -322,12 +322,21 @@
         });
         return deferred;
     } else {
-        var result = jQuery.ajax({
+        var result;
+        // jQuery.ajax returns the XHR object, even for synchronous requests,
+        // but in that case, the success callback will be called before
+        // jQuery.ajax returns. The first argument of the callback will be
+        // the server result, interpreted by jQuery according to the reponse's
+        // content-type (i.e. json or xml)
+        jQuery.ajax({
             url: url,
             type: (reqtype || 'GET').toUpperCase(),
             data: form,
             traditional: true,
-            async: false
+            async: false,
+            success: function(res) {
+                result = res;
+            }
         });
         return result;
     }