While developing an API for a client site I recognized that there was a tag being returned after our JSON response (generated using BaseController's returnJson method).

I copy & pasted some of the output into The Google and found a reference to the YII_DEBUG constant. I checked the value (true) and assumed it was related to devMode. I turned off devMode for this environment and the extra code went away (and YII_DEBUG===false).

I don't suspect that this extra output is expected while using the returnJson() method but I need to get around this. Can I turn off devMode outside of the config file? I really only want to make an exception when calling this method.

  • The question has been answered but I'd love to hear from P&T on whether or not this may change in a future release Jul 11 '14 at 2:35
  • What exactly is the extra output? Is it a bunch of console.log() calls (the profiler info)? Jul 11 '14 at 3:07
  • Yup, that's correct. There are a bunch of console.log calls Jul 11 '14 at 9:17
  • Got it. Right now Craft will only hide those if it’s an Ajax request. Would make sense that it’s based on the MIME type instead, though. Jul 11 '14 at 16:17
  • I agree. I can't think of a situation where I would actually want that output in my JSON file :) Jul 11 '14 at 16:38

I had the same problem. See Brad's response here to so this without messing with devMode: Console script tags garbling up non-html output (e.g. json) when in devMode

It would be nice if this happened automatically when specifically outputting non-html, though.


As of Craft 2.2, Craft will now only add the logs to the response if the Content-Type header is going to be "text/html" or "application/xhtml+xml". Should solve this for you.


You can turn off dev mode for just this page/call if you'd like, using the config->set API method.

craft()->config->set('devMode', false);

Don't forget to turn it back on after your function runs (if it still matters at that point).

  • Thank you! I'm setting devMode=false right before calling the BaseController::returnJson method. This is okay because the returnJson method spins down craft with craft->end() after echo'ing the json encoded data. Jul 11 '14 at 2:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.