9

I have this code as a cffunction that works fine:

<cfcomponent extends="core.core">

<cffunction name="loadService" access="remote" returnformat="JSON">

    <cfscript>

        objResponse = '{"CONFIG":[["internal"],[ "success"]],"DATA":[["Message1"]]}';

    </cfscript>

<cfreturn objResponse>  

</cffunction>   

</cfcomponent>

I am trying to convert it to a full cfscript function like this:

component extends="core.core"{

remote JSON function loadService(){

    objResponse = '{"CONFIG":[["internal"],[ "success"]],"DATA":[["Message1"]]}';

    SerializeJSON(objResponse);

    return objResponse; 
}

}

The first way returns JSON fine and I can process it with jQuery. The second one throws and error "The value returned from the loadService function is not of type JSON."

I have tried it with and without SerializeJSON and both ways throw that error. I have also tried it without specifying JSON in the function syntax. That does not throw an error but it does wrap wddxpacket info around it. This is what it looks like when I don't specify JSON:

<wddxPacket version='1.0'><header/><data><string>{"CONFIG":[["internal"],[ "success"]],"DATA":[["Message1"]]}</string></data></wddxPacket>

I am stuck on this. Any help would be great. Thanks!

3 Answers 3

15

The correct CFScript syntax in CF9 is:

remote any function loadService() returnformat="JSON" {

Technically, "JSON" is not a valid returntype from a function (see here for all returntypes), but when you write:

remote JSON function

...you're basically saying that.

Notice in your tag-based cffunction call, you do not specify a returnType...so guess what it is by default? (hint: any).

It's easy to mix returnType and returnFormat up. A simple adjustment above and you should be good to go.

Complete Code

component extends="core.core" {

remote any function loadService() returnFormat="JSON" {

    objResponse = '{"CONFIG":[["internal"],[ "success"]],"DATA":[["Message1"]]}';

    SerializeJSON(objResponse);

    return objResponse; 
}

}
Sign up to request clarification or add additional context in comments.

5 Comments

Wow, I see now. I was mixing up the returntype and return format. That was stupid. I added the returnformat="JSON" and it worked great. Thanks for the help. I really appreciate it.
Definitely take note of Peruz's comment about SerializeJSON().
You said "The correct CFScript syntax in CF9 is:", is there some sort of documentation for CFScript... how did you know this?
If you can believe it, I tracked the correct syntax down from the CF9 bug database on Adobe.com: cfbugs.adobe.com/cfbugreport/flexbugui/cfbugtracker/…
@ShawnHolmes there is a brief mention of this syntax in Developing Adobe ColdFusion 10 Applications, but of course it has no details.
3

Also, I noticed that you have

SerializeJSON(objResponse);

in your function. This line has no effect on your function's return. So, it can easily be ommitted as your objResponse value is already in a JSON string. But, if the value of objResponse was something like

objResponse = {
    "CONFIG" = [["internal"], ["success"]],
    "DATA" = [["Message1"]]
};

then you could have done something like

return serializeJSON(objResponse);

which would have turn the complex data you had into a JSON string.

Here's the complete function

remote any function loadService()
    returnFormat="JSON"
{
    objResponse = {
        "CONFIG" = [["internal"], ["success"]],
        "DATA" = [["Message1"]]
    };

    return serializeJSON(objResponse);
}

1 Comment

Thanks for the feedback. I was actually playing with the placement of the serializeJSON. This helps
1

Another way to specify the 'returnFormat' would be to use annotations:

component extends="core.core" {
/**
 * @hint         loads properties of an object and returns them in as JSON
 * @output       false
 * @returnFormat JSON
 */
remote struct function loadService() {
  objResponse = {
    CONFIG = [["internal"],[ "success"]],
    DATA = [["Message1"]]
  };
  return objResponse; 
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.