javascript - Async AJAX call not working with when and then -
i have following code:
function accesscontrol(userid) { return $.ajax({ url: "userwidgets", type: "get", datatype: 'json', data: { userid: userid } }); }; var userwidgets = accesscontrol('1'); $.when(userwidgets).then(function (data) { alert(data); });
i don't want make function sync adding parameter async: false
, alert isn't appearing @ all, there wrong in code? should use approach?
$.ajax
returns promise. so, don't need wrapped in $.when
again.
use then
directly on userwidgets
ajax
instance.
userwidgets.then(function (data) { alert(data); }, function(e, status, error) { console.log(error); // debugging });
from jquery docs
jqxhr.then(function( data, textstatus, jqxhr ) {}, function( jqxhr, textstatus, errorthrown ) {});
incorporates functionality of
.done()
,.fail()
methods, allowing (as of jquery 1.8) underlying promise manipulated. referdeferred.then()
implementation details.
Comments
Post a Comment