1
votes

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.

2
You have returnFormat="void". Void is for returnType. The returnFormat should be json,wddx,or plain. - Leeish
Yes I need to take the result but I can't use Ajax in this case. Is there any other way to handle response in this case? - espresso_coffee
What do you mean you can't use ajax. You can control the javascript obviously. It's unclear what you are attempting to do with the data from getRecords. Are you trying to send the user to a page? Show the records on the page? Send the user to a page based on the user name? In the code provided, your function does not make a call to the cfc page. - Leeish
Just to check if my result is success = 1 or alert error if it's 0. Yes I can't use ajax because that cause some other problems in my code. I have other stuff included in my cffunction. - espresso_coffee
Your code is not calling that page. It's just assigning a string to the variable location.href. Do you have more code that would have this make more sense. If you want your javascript to know if that call results in a 1 or a 0, you have to call the page behind the scenes, AJAX. - Leeish

2 Answers

3
votes

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;
}
2
votes

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.
    }
  }
};