Copy link to clipboard
Copied
i proxied a call to a cfc which does an ftp get to a local temp file. Then it reads the file and is supposed to return the file content as a cfreturn string. i know the code works, when i pull it out of cfc into a regular cfm file it does exactly what is expected
however, in my proxy callBackHandler, the cfc return is null.
<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);
}
function ftpSuccess(ftpReturn)
{
if (ftpReturn.length==0)
// error thrown right here: "ftpReturn is Null"
{
alert("Your FTP Get returned a blank file");
}
}
ion wrote:
can i prevent it from executing onLoad?
Hi ion,
You're welcome, and yes. Can you please try this:
Application.cfc
----------------------
component {THIS.name = "TestCFAjaxproxyCFC";}
text.txt
----------------------
my text
MyCFC.cfc
----------------------
component {remote string function myCFCFunction(required string myArg) {return fileRead(expandPath("./text.txt")) & '-' & ARGUMENTS.myArg;}}
index.cfm
----------------------
<cfajaxproxy cfc="MyCFC" jsclassname="myCFCJSObj" />
<script type="text/ja
...Copy link to clipboard
Copied
ion wrote:
<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);
}
Shouldn't lpar ,remoteFile, userName, password also be arguments of getFTP?
Copy link to clipboard
Copied
they are, i didn't include the first part of the func, which gets those values from some form fields.
that part works, i know that because if i comment out the line instance.setCallbackHandler(ftpSuccess) i can see in fireBug, under the http\Response tab the actual file content, which is "test"
that's why i don't get the "null" error, looks like there is some particular syntax that must be used in order to handle the return inside the proxy
Copy link to clipboard
Copied
The intention seems to have been
if (ftpReturn.length != 0) alert ('OK');
Or, to cover both eventualities,
if(ftpReturn.length==0)
{
alert("FTP Get returned blank file");
}
else
{
alert("FTP Get returned non-blank file");
}
Edited by BKBK
Copy link to clipboard
Copied
thanks, but actually no, the intention is to dispaly the file content as the innerHTML of a div, obviously not possible since the function gets a null, which means, it doesn't even get the cfreturn (blank or no blank, it just doesn't get anything)
Copy link to clipboard
Copied
just to make sure everything else is ok, i've added a cfinvoke statement on my page, invoking the same function and passing the same parameters. it behaves exactly as expected, the return string from cfc gets displayed on the page using a cfoutput. it's strictly a proxy thing, the way that callbackhandler is supposed to work
Copy link to clipboard
Copied
ion wrote:
just to make sure everything else is ok, i've added a cfinvoke statement on my page, invoking the same function and passing the same parameters. it behaves exactly as expected, the return string from cfc gets displayed on the page using a cfoutput. it's strictly a proxy thing, the way that callbackhandler is supposed to work
Did you do something like this?
<script>
...
...
function ftpSuccess(ftpReturn)
{
document.getElementById('result').innerHTML = ftpReturn;
}
...
...
</script>
<!--- Within body of page --->
<div id="result"></div>
If so, what was the result?
Copy link to clipboard
Copied
sorry if i confused things. the callBackHandler function never gets to the alert (or setting the innerHTML) because it throws the null object error on the if(ftpReturn.length==0) condition, which means it doesn't even see the cfreturn object coming from the cfc.
if i remove the conditional and simply try to set the innerHTML, i get a http array with 2 elements:
0 null
1 undefined (and of course is not setting the innerHTML)
thanks
Copy link to clipboard
Copied
ion wrote:
// error thrown right here: "ftpReturn is Null"
[...]
if i comment out the line instance.setCallbackHandler(ftpSuccess) i can see in fireBug, under the http\Response tab the actual file content, which is "test"
[...]
if i remove the conditional and simply try to set the innerHTML, i get a http array with 2 elements:
0 null
1 undefined (and of course is not setting the innerHTML)
Hi ion,
I'm confused. Could you please run the following app, and let us know the JS alert message?
Application.cfc
----------------------
component {THIS.name = "TestCFAjaxproxyCFC";}
text.txt
----------------------
my text
MyCFC.cfc
----------------------
component {remote string function myCFCFunction() {return fileRead(expandPath("./text.txt"));}}
index.cfm
----------------------
<cfajaxproxy cfc="MyCFC" jsclassname="myCFCJSObj" />
<script type="text/javascript">
function myCallbackHandler(result) {
alert(result.length);
}
function myErrorHandler(statusCode, statusMsg) {
alert('Status: ' + statusCode + ', ' + statusMsg);
}
function myJSFunction() {
var instance = new myCFCJSObj();
instance.setCallbackHandler(myCallbackHandler);
instance.setErrorHandler(myErrorHandler);
instance.setReturnFormat('plain');
instance.myCFCFunction();
}
myJSFunction();
</script>
Thanks,
-Aaron
Copy link to clipboard
Copied
it works Aaron, alert returns the correct result.length, 7
is this my problem, the syntax used inside the cfc?
thanks a lot
Copy link to clipboard
Copied
i think i understand what happens, but i'm not really happy about it. The reason it works is you call the function right after you declare it. I guess this is kind of a substitute for the ajaxonload function. My problem is, i don't want the function to execute onLoad, because it needs some user selected values from the form, to pass as parameters to the cfc
if i do the same thing (call the function after declaring it), i get another "Null" error, this time about the form fields that haven't been set by user yet. after that, the function will work
can i prevent it from executing onLoad?
thanks again
Copy link to clipboard
Copied
ion wrote:
can i prevent it from executing onLoad?
Hi ion,
You're welcome, and yes. Can you please try this:
Application.cfc
----------------------
component {THIS.name = "TestCFAjaxproxyCFC";}
text.txt
----------------------
my text
MyCFC.cfc
----------------------
component {remote string function myCFCFunction(required string myArg) {return fileRead(expandPath("./text.txt")) & '-' & ARGUMENTS.myArg;}}
index.cfm
----------------------
<cfajaxproxy cfc="MyCFC" jsclassname="myCFCJSObj" />
<script type="text/javascript">
function myCallbackHandler(result) {
alert(result);
}
function myErrorHandler(statusCode, statusMsg) {
alert('Status: ' + statusCode + ', ' + statusMsg);
}
function myJSFunction() {
var instance = new myCFCJSObj();
instance.setCallbackHandler(myCallbackHandler);
instance.setErrorHandler(myErrorHandler);
instance.setReturnFormat('plain');
instance.myCFCFunction(document.getElementById("myfield").value);
}
//myJSFunction();
</script>
<cfform>
<cfinput type="text" name="myfield" value="foobar" />
<cfinput type="button" name="mybutton" value="submit" onclick="javascript:myJSFunction()" />
</cfform>
Clicking 'submit' should alert "my text-foobar".
Thanks,
-Aaron
Copy link to clipboard
Copied
many thanks Aaron, works fine. although now i'm a little confused: i've kind of ended up with the same code i started with, didn't figure out yet what i was doing wrong
the only thing i've added is an error trap in the js function, like:
if (lpar != '' && remoteFile != '' && userName != '' && password != '')
{
... set the proxy ...
} else { alert("Missing fields!"); }
and another one in the cfc, if the file returned is, for some reason, blank. If that happens, then the "null" message is actually correct:
<cfset ftpReturn = fileRead(tempFile)>
<cfif trim(ftpReturn) eq ''>
<cfset ftpReturn="FTP get operation returned a Blank file">
</cfif>
thanks again everybody for your help, best regards
ion
Copy link to clipboard
Copied
ion wrote:
thanks again everybody for your help, best regards
Very cool, glad it's working now!
Copy link to clipboard
Copied
ion wrote:
thanks, but actually no, the intention is to dispaly the file content as the innerHTML of a div, obviously not possible since the function gets a null, which means, it doesn't even get the cfreturn (blank or no blank, it just doesn't get anything)
Your post has until now made it seem otherwise. You made it seem, to me at least, that the problem is that you were failing to get the alert! So, let me ask. Does the alert appear as expected?
Copy link to clipboard
Copied
ion wrote:
that's why i don't get the "null" error, looks like there is some particular syntax that must be used in order to handle the return inside the proxy
The lights flickered when I read this again. I was reminded of a recommendation I had seen somewhere to use the following function syntax instead:
var getFTP = function() {}
var ftpSuccess = function(ftpReturn) {}
Could that be the solution?