如何从coldfusion cfc页面获取返回变量?

时间:2016-09-23 16:11:07

标签: javascript coldfusion cffunction

我有调用.cfc pag的函数。我传递方法和参数以及页面名称。这是我的代码:

function callFunction(name){
    param = name;

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

这是我在cfc页面上的功能:

<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>

我打电话给我的函数后,我的代码没有给我返回变量。我不确定我的代码中缺少什么。如果有人可以帮助解决这个问题,请告诉我。

2 个答案:

答案 0 :(得分:3)

我不确定我是否理解这个问题,但您是否在寻找这个问题?

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

  location.href = target;

  return target;
}

答案 1 :(得分:2)

这是从getRecords组件中获取myTest.cfc结果的方法。

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