ajax - jQuery: weird behavior - XHTTPResponses vs promises/wrappers -
having code:
(function($) { $(function(){ $("div.cotizador ul > li > a.cotizador").click(function(){ var data = { concesionaria: $(this).data("concesionaria"), modelo: $(this).data("modelo") }; var promise = $.get("ir-a-concesionaria", data, function(data){ if ("success" in data) { window.location.href = data.success.redirect; } else { alert("no se puede seleccionar el modelo especificado. consulte con el administrador del sitio."); } }, "application/json"); console.log(data); console.log(promise); promise.fail(function(xhr, errortype, errordescription){ alert("ocurrió un error interno. intente nuevamente más tarde"); }); }); }); })(jquery);
(currently doesn't matter html triggers callback).
when instruction console.log(promise)
reached, google chrome shows in console debug message:
xmlhttprequest {statustext: "", status: 0, response: "", responsetype: "", responsexml: null…}
this means: returned result $.get not promise-wrapped xhttp (and so, .fail method not exist).
meanwhile, debugging data
object gives {modelo:"xxx", concesionaria:"yyy"}
assuming "xxx" , "yyy" have well-defined , expected values (this means: data defined , has no errors when error i'm talking triggered).
however when copy-paste code console, , replace data
argument literal object, e.g.:
jquery.get("ir-a-concesionaria", {modelo:'toyota-gt86', concesionaria:'casabaca'}, function(data){ if ("success" in data) { window.location.href = data.success.redirect; } else { alert("no se puede seleccionar el modelo especificado. consulte con el administrador del sitio."); } }, "application/json");
the returned object (as seen in google chrome's console) promise-wrapped object (as told in documentation).
question: why happening?
(even when non-helpful: i'm using drupal 7)
found answer (drupal sucks):
first, 1.4.4 version of jquery loaded. then, function called. finally, 1.10.1 version of jquery loaded. after that, repeated test case and, of course, worked -.-''.
Comments
Post a Comment