Hung Pham
Hung Pham

Reputation: 33

ColdFusion Prefix serialized JSON with

In the CF 2016 Administrator page, by selecting the check box "Prefix serialized JSON with" //ABC (form example), it will break the function below, because it will add the string //ABC to the JSON

How can we remove the prefix //ABC before parsing the JSON, please?

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

     <cfquery name="getData" datasource="#dataSource#">
          SELECT *
          FROM aTable
     </cfquery>

     <cfreturn serializeJSON(getData)>

</cffunction>

Thank you for your help

Upvotes: 1

Views: 436

Answers (2)

Thiraviam
Thiraviam

Reputation: 61

We have an option for this prefix Serialized JSON uncheck the Prefix serialized JSON with options in our admin comes under the options cfadmin - > Server Settings - > setting table you can see that options. FYR, Please refer my below images & sample codes,

Before uncheck the prefix option:

<cfset struct = {"name":"thiraviam", "age":24}>
<cfdump var="#serializeJSON(struct)#">

Output:

//abc{"name":"thiraviam","age":24}

After uncheck the prefix option:

<cfset struct = {"name":"thiraviam", "age":24}>
<cfdump var="#serializeJSON(struct)#">

Output:

{"name":"thiraviam","age":24}

[Image of CF Admin Settings]

I hope it's help you more. Thank you !

Upvotes: 6

Miguel-F
Miguel-F

Reputation: 13548

You did not provide how you are calling this service. Regardless, all you need to do when calling a service which prefixes the JSON data is remove the prefixed data before processing the response. Below is an example using jQuery to make an AJAX call to such a service.

For jQuery AJAX the key for this is to use the dataFilter option. The dataFilter option gives you access to the raw response so it can be sanitized.

A function to be used to handle the raw response data of XMLHttpRequest. This is a pre-filtering function to sanitize the response. You should return the sanitized data. The function accepts two arguments: The raw data returned from the server and the 'dataType' parameter.

From the jQuery documentation

Here is an example. Notice how the dataFilter in this case is removing the first 2 characters from the response with this code; data.substr(2). For your example you would need to increase that to 5 in order to remove //ABC.

$.ajax({
    type: 'POST',
    cache: false,
    data: {property1:'somevalue',property2:'someothervalue'},
    dataFilter: function(data, type) {return data.substr(2)},
    dataType: 'json',
    url: 'https://www.somedomain.com/api/service/',
    success: function(data, stat, resp) {
        if(!data.error) {
            // good return so do what you need to do
            // this is assuming the service returns an 'error' field
            // the JSON data is accessible by using data.fieldname format
        } else {
            // bad return so trap it
        }
    },
    error: function(resp, stat, err) { 
        // service call failed so trap it
    }
});

Upvotes: 3

Related Questions