1

我代理了对 cfc 的调用,该 cfc 对本地临时文件执行 ftp“获取”。然后它读取文件并应该将文件内容作为字符串返回。我知道代码有效,当我将其从 cfc 中拉出到常规 cfm 文件中时,它完全符合预期。但是,在我的代理callBackHandler中,cfc 结果似乎为空。这是代理代码:

    <cfajaxproxy cfc="ftpfunc" jsclassname="jsobj" />
    function getFTP() {
       ...
       var instance = new jsobj();
       instance.setCallbackHandler(ftpSuccess);
       instance.setErrorHandler(ftpError);
       instance.setReturnFormat('plain');
       instance.getJCL(lpar,remoteFile,userName,password); 
    }

.. 然后是 callbackHandler,它应该有从 cfc 返回的字符串:

    function ftpSuccess(ftpReturn)
    {
     // error thrown right here: "ftpReturn is Null"
       if (ftpReturn.length==0)
           { alert("Your FTP Get returned a blank file"); }
     }

是否有要使用的特定语法?例如,当返回类型是结构或查询时,您必须使用类似ftpReturn.DATA. 直弦呢?

谢谢你的帮助

编辑:这是cfc

<cffunction name="getJCL" output="false" access="remote" securejson="true">
    <cfargument name="lpar" type="string" required="yes">
    <cfargument name="remoteFile" type="string" required="yes">
    <cfargument name="userName" type="string" required="yes">
    <cfargument name="password" type="string" required="yes">
    <cfset var ftpReturn = "">

    <cfftp action="open"
        connection="getConnection"
        password="#arguments.password#"
        secure="yes" 
        server="#arguments.lpar#" 
        stopOnError="no" 
        timeout="30" 
        username="#arguments.username#">

    <cfset tempFile="D:\myDir\#RandRange(10000000,99999999)#.tmp">

    <cfftp action="getFile"
        connection="getConnection"
        localFile="#tempFile#"
        remoteFile="#arguments.remoteFile#"
        transferMode="auto">

    <cffile action = "read" file = "#tempFile#" variable = "Message">

    <cfset ftpReturn = Message>

    <cfreturn ftpReturn>
</cffunction>
4

1 回答 1

0

if i remove the callbackHandler function, i can see in the Console\Response tab of fireBug the actual file content, which is "test"
...
i've installed fiddler too, under the json tag i only get "The response does not contain valid JSON text"

So you're not currently returning valid JSON?

It may or not be a bug in CF that it passes null if it receives invalid content (rather than throwing an error), but change the file contents to something that validates and maybe it'll work?


You could also try something like...

<cftry>
    <cfset deserializeJson(ftpReturn) />
    <cfcatch>
        <cfset ftpReturn = '{"error":"invalid json"}' />
    </cfcatch>
</cftry>

...to detect inside the function if it's valid JSON and if not return appropriate message.


Don't forget you need output=false on the cfcomponent tag itself, otherwise you might still get whitespace added from the area outside the functions.


A couple of other things - you're not actually using the Message variable, so just do:

<cffile action = "read" file = "#tempFile#" variable = "ftpReturn">

Also, random numbers are not guaranteed to be unique, so there's a small chance of getting data corruption unless you switch to:

<cfset tempFile="D:\myDir\#createUuid()#.tmp">
于 2012-06-30T14:09:40.763 回答