1

Using this AJAX call with async set to true causes the following function (see below) to fail with undefined error. Clearly it is because the AJAX call is not getting a return value before the browser attempts to use it. If async is set to false this AJAX function works. What am I missing?

function AjaxCmd(cmd) {
var txtRetval;
//alert(cmd);
$.ajax({
    type: "POST",
    url: "/telligent/rpjquery-functions.asp",
    data: "cmd=" + cmd,
    datatype: "html",
    //async Not supported in IE8 or IE9 11082014
    cache: false,
    async: true,
    success: function(data) {
        //alert("success");
        txtRetval = data;
   },
    error: function (request, status, error) {
        //alert(request.responseText);
        txtRetval = false;
    },
    failure: function() {
        //alert("failure");
        txtRetval = false;
    }
});
//return callback( txtRetval ); 
if (txtRetval == "not authenticated") {
    jQT.goBack("#");
    location.reload(true);
    alert(txtRetval);
    return (false);
} else {
    //
}
return (txtRetval);

}

This AJAX call will fail with: unable to get property length of undefined or null reference. This call will work with async set to false:

        var filterstring = AjaxCmd("getfilterstring&intFormID="+intFormID+"&SF="+SF+"&txtFormType="+txtFormType+"&a="+a+"&s="+s);
        var filterstringlength = filterstring.length
        for (var x = 1; x <= filterstringlength; x++)
Phil
  • 157,677
  • 23
  • 242
  • 245
Modulus
  • 13
  • 3
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Phil Sep 13 '16 at 03:04
  • 1
    Note, `failure` is not a default `$.ajax()` option – guest271314 Sep 13 '16 at 03:19

0 Answers0