2016-09-23 80 views
1

我有打电话给.cfc页面的功能。我传递方法和参数以及页面名称。这里是我的代码:如何从coldfusion cfc页面获取返回变量?

function callFunction(name){ 
    param = name; 

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

这是我的CFC页cffunction:

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

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

+0

你有'returnFormat =“void”'。 void为returnType。 'returnFormat'应该是'json','wddx'或'plain'。 – Leeish

+0

是的,我需要结果,但在这种情况下我不能使用Ajax。在这种情况下是否有其他方式来处理响应? –

+0

你是什么意思,你不能使用阿贾克斯。显然你可以控制javascript。目前还不清楚你试图用'getRecords'中的数据做什么。你是否试图将用户发送到一个页面?在页面上显示记录?根据用户名称将用户发送到页面?在提供的代码中,你的函数不会调用cfc页面。 – Leeish

回答

3

不知道我已经理解了这个问题,但你在找这个...?

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

    location.href = target; 

    return target; 
} 
+0

我试过了你的代码,在调用之后仍然没有获得目标值。 –

+0

什么叫。呼叫发生在哪里espresso?你可以在你“调用”函数的地方发布你的代码吗? – Leeish

+0

@Leeish我没有使用ajax来调用我的函数,但在上面的函数中我有location.href和指向我的函数和传递参数的方法。那么在这种情况下,是否可以从函数结果得到响应呢?我无法使用Ajax,因为我在同一页面上具有电子表格的功能,并且不会与ajax一起使用。谢谢。 –

2

这是你将如何从myTest.cfc成分提取的getRecords结果。

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

没有办法从你的cfc中获取数据到你的javascript而不使用异步调用。像在你的例子中那样给一个变量分配一个URL不会。所以你要么不理解AJAX是什么和用于什么,不能显示你的所有代码,或者不传达你想要做的事情。 – Leeish