0

I built a SessionMgr.cfc

<cffunction name="jgetValue" access="remote" returntype="string" output="yes" returnFormat="json">
    <cfargument name="variablename" type="string" required="yes">
    <cfset var result = 0>
    <cfset result = Evaluate("session" & "." & arguments.variablename)>
    <cfset var ReturnValue = result />
    <cfreturn result />
</cffunction>

in coldfusion to set/get session variables to run all my $.ajax calls, And I seem to be doing something wrong. I have read and read stackoverflow and every page google can produce on the subject, perhaps someone here can explain what I am doing wrong.

here is my getter();, my getValue(), and getCalBack();, the correct value is correct in getValue() but everything I have tried returns [object Object] to the getCAllBack() handler; here is my code;

// rID in this instance is session.rID
var tReportID = getValue('rID');
alert(tReportID);

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

any help would be appreciated. So I updated the code to your suggestion Prince, however I still get [object Object] in the alert.

If I breakdown the object, how do I get the responseText out? it has the right value; responseText: "12"

BigBear
  • 59
  • 4
  • 90% of the time the return value is a simple numeric value, the other 10% is a single string value like 'Select', if that helps any. – BigBear Mar 13 '20 at 17:20
  • All that means is the tool you're using i.e. alert() doesn't know how to display the contents of complex objects, like a structure, so it uses the default ["object object"](https://stackoverflow.com/questions/4750225/what-does-object-object-mean). For complex objects, use `console.log( obj )`. Also, it's better to use jquery than rely on the questionable implementation of Adobe's ajax components... Please post the code for SessionMgr.cfc so someone can help you resolve the issue with the jquery code. – SOS Mar 14 '20 at 01:48

2 Answers2

0

use cfajaxproxy instead

<cfajaxproxy cfc="CFC/SessionMgr"       jsclassname="SessionMgr" />

then in

<script>
var s = new SessionMgr();
s.setValue('rID', x);
ReportID = s.getValue('rID');
</script>
BigBear
  • 59
  • 4
  • Do yourself a favor and don't use CF's ajax components. They're outdated and buggy IMO. Please update your question with the SessionMgr.cfc code and someone can help you resolve the original issue. – SOS Mar 14 '20 at 01:43
  • well, it wasnt being explained how to do this simply with ajax, and this works, so.. – BigBear Mar 14 '20 at 14:26
  • Adobe's ajax stuff is notoriously buggy, so the recommendation is to avoid using it with new applications. Yeah, the explanations weren't clear, but looks like you weren't that far off. Sounds like the json response is a structure `{responseText: "12"}`, you need to specify the name of the key containing the value, i.e. In your example, use `obj.responseText`. (See also the other comments about why you were seeing "[object object]" ) – SOS Mar 15 '20 at 01:10
  • The problem is ajax calls are *asynchronous* by default, but the code is using it as if it were synchronous (waits for a response). You must specify `asynch: false`. I don't normally use it that way, but [here's a working example](https://pastebin.com/TNgFFVNc). Depending on what you're doing with the values, might cleaner to just do whatever is needed with response inside the success() callback, [like this](https://pastebin.com/xN8sxqP8). – SOS Mar 16 '20 at 07:44
  • omg, I knew I was missing the forest thru the trees, I will implement this and comment again when I get it working. It has been ~ 8 years since I dabbled in ajax and coldfusion, so bear with my rust. thanks again Ageax. – BigBear Mar 16 '20 at 12:04
  • thank you SO much Ageax for the effort you put into this answer, it works perfectly, I am currently rewriting the engine to remove all references to cfajaxproxy and replacing with proper ajax calls, I owe you a beer. – BigBear Mar 16 '20 at 12:24
  • Glad it helped! I just realized the 2nd link was broken. Here it is again. I usually use asynchronous ajax calls and do the work within the success callback method (or call another method within success) https://pastebin.com/E1z04UyH – SOS Mar 16 '20 at 18:01
  • is the callback faster performance wise? I am working on a ReportBuilder that cascades Reports to Lines to Nodes, and now that I have it working correctly, I am seeing a bit of a performance issue in the way I am doing it. It all works well, but being a perfectionist, I would like for it to be screaming fast. I will post another question concerning it. – BigBear Mar 17 '20 at 13:03
  • AFAIK, no. A call to the server takes what it takes in terms of time. If the server is slow, making 10 or 12 calls may take longer. So if you're just returning a bunch of static data (small queries), you might consider consolidating the number of ajax calls. Also, I noticed your other question is still using `asynch : false`, which kind of defeats the purpose of using ajax :-) Only use it if needed. – SOS Mar 20 '20 at 00:51
0

Try changing:

dataType: "text",

to:

dataType: "json",
gustavosmanc
  • 51
  • 1
  • 5