0

I am using session scope in ColdFusion and have created an AJAX setter and getter. When I call the jQuery function for the getter I get undefined, but inside the jQuery call I write to the console and the values are correct. I have tried async: false in the AJAX to no avail.

I am building a complex cascade of linked select objects and need for them to default back to the last selected values when reloading the cascade. Has anyone encountered this? I have read everything I can find on StackOverflow and have not been able to solve this, see code example below

AJAX types of GET vs POST, AJAX async: false, sessionManager and pretty much everything else I found here.

By default session.ReportsShowAll = 1, but could change, so I use jgetValue() to get the most recent value and while creating the select, set the selected option to whatever session.ReportsShowAll currently is. This logic is populated in a variety of areas but I am troubleshooting the one instance to predicate elsewhere.

The AJAX call writes to the console:

jgetValue: ReportsShowAll value: "1"

However the var sel setting gets undefined.

jgetValue = function(a) {
  $.ajax({
    url: "cfc/SessionMgr.cfc",
    type: "get",
    //async: false,
    dataType: "text",
    data: {
      method: "jgetValue",
      variablename: a
    },
    success: function(response) {
      console.log('jgetValue: ' + a + ' value: ' + response);
      return response;
    },
    error: function(msg) {
      console.log(msg);
    }
  });
}

here is the modified callback system written according to the duplicate thread.

getter = function(a) {
    var callback = jgetValue(a);
    return callback;
}


function getCallBack(response) {
    console.log('callback handler: ' + response);
    return response;
}

callback handler: "1"

  jgetValue = function(a) {

     $.ajax({
            url: "cfc/SessionMgr.cfc",
            type: "get",
            //async: false,
            dataType: "text",
            data: {
                method: "jgetValue",
                variablename: a
            },
          success: getCallBack //function(response) {
            //console.log('jgetValue: ' + a + ' value: ' + response);
           // getCallBack();
            //return response;
          //}
              ,
          error: function(msg) {
              console.log(msg);
          }
      });

}

both methods, before the async call and after the call still do not set the value to the javascript object, still considers it undefined.

var sel = jgetValue('ReportsShowAll');
BigBear
  • 59
  • 4
  • jgetValue = function(a) { $.ajax({ url: "cfc/SessionMgr.cfc", type: "get", //async: false, dataType: "text", data: { method: "jgetValue", variablename: a }, success: function(response) { console.log('jgetValue: ' + a + ' value: ' + response); return response; }, error: function(msg) { console.log(msg); } }); } – BigBear May 28 '19 at 16:28
  • You cannot return the response from an async call, and `async: false` is not a workable solution. You need to re-write your logic to work with callbacks instead. See the duplicate for more information – Rory McCrossan May 28 '19 at 16:32
  • thank you sir, I did not see this posting – BigBear May 28 '19 at 16:33
  • could you provide a link to the duplicate you speak of – BigBear May 28 '19 at 16:34
  • Link is in the yellow box above your question – Rory McCrossan May 28 '19 at 16:36
  • Im new, apologies, I did find it by actually looking – BigBear May 28 '19 at 16:38
  • ok, so I built similar to the suggestion topic posted by Rory, tx for that. I created a function to call the async ajax call, and I still get the javascript obj as undefined. So I wrote a callback function into the ajax call success step. This is how I need it to work, I send a couple of variable values, and expect a return for this combination. Even in the callback it writes the value to console, and is set to return the same value, yet the javascript obj is still undefined. – BigBear May 28 '19 at 18:28

0 Answers0