Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON response using cfscript function

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!

like image 618
Sequenzia Avatar asked Jan 05 '12 04:01

Sequenzia


3 Answers

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

}
like image 78
Shawn Holmes Avatar answered Sep 24 '22 03:09

Shawn Holmes


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);
}
like image 36
Peruz Carlsen Avatar answered Sep 26 '22 03:09

Peruz Carlsen


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; 
}
}
like image 34
jsjs94 Avatar answered Sep 26 '22 03:09

jsjs94