espresso_coffee
espresso_coffee

Reputation: 6110

How to get return variable from coldfusion cfc page?

I have function that makes call to .cfc pag. I'm passing method and parameter along with the page name. Here is my code:

function callFunction(name){
    param = name;

    location.href = 'myTest.cfc?method=getRecords&userName=' + param;
}

Here is my cffunction on cfc page:

<cfcomponent>
    <cffunction name="getRecords" access="remote" returnformat="void">
        <cfargument name="userName" type="string" required="yes">

        <cfset myResult = "1">

        <cftry>
            <cfquery name="getResults" datasource="test">
                //myQuery
            </cfquery>

            <cfcatch>
                <cfoutput>#cfcatch#</cfoutput>
                <cfset myResult="0">
            </cfcatch>
        </cftry>
        <cfreturn myResult>
    </cffunction>
</cfcomponent>

My code doesn't give me return variable after I make a call to my function. I'm not sure what I'm missing in my code. If anyone can help with this problem please let me know.

Upvotes: 1

Views: 1096

Answers (2)

Leeish
Leeish

Reputation: 5213

This is how you would fetch the result of getRecords from the myTest.cfc component.

var xhr = new XMLHttpRequest();
xhr.open('GET', 'myTest.cfc?method=getRecords&userName='+name);
xhr.send(null);

xhr.onreadystatechange = function () {
  var DONE = 4; // readyState 4 means the request is done.
  var OK = 200; // status 200 is a successful return.
  if (xhr.readyState === DONE) {
    if (xhr.status === OK) 
      var result = xhr.responseText; // 'This is the returned text.'
      //result will = 1 or 0.
    } else {
      console.log('Error: ' + xhr.status); // An error occurred during the request.
    }
  }
};

Upvotes: 2

Valentin Roudge
Valentin Roudge

Reputation: 565

Not sure I've understood the question but are you looking for this... ?

function callFunction(name) {
  var target = 'myTest.cfc?method=getRecords&userName=' + name;

  location.href = target;

  return target;
}

Upvotes: 3

Related Questions