1

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.

espresso_coffee
  • 5,980
  • 11
  • 83
  • 193
  • You have `returnFormat="void"`. Void is for returnType. The `returnFormat` should be `json`,`wddx`,or `plain`. – Leeish Sep 23 '16 at 16:14
  • 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 Sep 23 '16 at 16:19
  • 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 Sep 23 '16 at 16:20
  • 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 Sep 23 '16 at 16:22
  • 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 Sep 23 '16 at 16:22
  • Yes my code calls that page and specific function on that page. I'm just wondering how to get return variable in this case. – espresso_coffee Sep 23 '16 at 16:24

2 Answers2

3

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;
}
Valentin Roudge
  • 555
  • 4
  • 14
  • I have tried your code and still did not get target value after the call. – espresso_coffee Sep 23 '16 at 16:24
  • What call. Where is the calling happening espresso? Can you post your code where you "call" the function? – Leeish Sep 23 '16 at 16:27
  • @Leeish I haven't use the ajax to make a call to my function but in the function above I have location.href and method that is pointing to my function and passing parameter. So in this case is it possible to get response from the function result or not? I can't use Ajax because I have function for spreadsheet on the same page and that will not work along with ajax. Thank you. – espresso_coffee Sep 23 '16 at 16:32
  • If you do not make a call to the page, no you cannot get a result from that page. The only way to make a call to that page is with AJAX. – Leeish Sep 23 '16 at 16:34
  • 1
    I think you are above your head in whatever it is you are trying to do because it's not making any sense. – Leeish Sep 23 '16 at 16:35
2

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.
    }
  }
};
Leeish
  • 5,203
  • 2
  • 17
  • 45
  • 1
    There is NO WAY to get data from your cfc into your javascript without using an asynchronous call. Assigning a URL to a variable as in your examples doesn't do it. So you're either not understanding what AJAX is and used for, not showing all your code, or not communicating what you are trying to do correctly. – Leeish Sep 23 '16 at 16:31