Adding JSON MimeType/ContentType to ColdFusion

Something that has bothered me for years about ColdFusion is that when you pass ?returnformat-json to a service call you get json sure, but the mimetype is text/plain. I have began adding it myself just because it annoys me so much.  I want the browser to know the type, which is helpful for many reasons – one of which is so that it is coming in as a native Javascript object.  Just adding a cfheader tag does the trick.

<cffunction name="getTest" returntype="string" access="remote" returnformat="json">
     <cfset var returnstruct = StructNew() />
     <cfset returnstruct["userid"] = "testing" />

     <cfheader name="Content-Type" value="application/json">
     <cfreturn SerializeJSON(returnstruct) />

Leave a Reply

Your email address will not be published. Required fields are marked *