Skip to main content
Inspiring
October 23, 2012
Answered

Is it possible to determine/set mimetype for CFC-generated content?

  • October 23, 2012
  • 1 reply
  • 694 views

In short, my onCFCRequest() method makes a manual invokation of the component and sends it the arguments.

Let's say, for example, I am calling a component located at www.coms.template

Is it possible for me to do something in a method called in that template component so that when it returns the generated content and we are now back in the onCFCRequest() method, that I can look at the generated content and determine/set/serve the appropriate mimetype?

My methods can generate JSON, WDDX, CSS, Javascript and HTML, and I want to make sure they serve as the appropriate mimetypes when their content is sent to the browser:

  • JSON = text/json
  • WDDX = text/xml
  • Javascript = application/x-javascript
  • CSS = text/css
  • HTML = text/html
  • Other = text/plain
    This topic has been closed for replies.
    Correct answer Adam Cameron.

    Well... you know what content you're generating, so the calling code needn't guess, you can just tell it. Return a struct which has the response data in one key, ans the type in another.

    And you set the MIME type for a response using <cfcontent>

    --

    Adam

    1 reply

    Adam Cameron.Correct answer
    Inspiring
    October 23, 2012

    Well... you know what content you're generating, so the calling code needn't guess, you can just tell it. Return a struct which has the response data in one key, ans the type in another.

    And you set the MIME type for a response using <cfcontent>

    --

    Adam

    Inspiring
    October 23, 2012

    Ah!  Like having the component.cfc set something into the URL scope (like a variable called 'returnType') so that when we get back to the onCFCRequest() it can read the value and say "Oh!  It looks like that component wanted to return it's data as: XML, I'll set the mimetype accordingly.

    That makes sense.

    ** UPDATE ** AND IT WORKS!

    Had to do some fannageling, but I got it.  It now properly outputs content in JSON (application/json), WDDX/XML (text/xml), Javascript (application/javascript), CSS (text/css), HTML (text/html) and Text (text/plain).

    I did a test and had the component generate HTML code but tell the oCFCR() to serve it up as text and sure enough, it did not evalute the paragraph tags, but displayed them like text.  BRILLIANT!