2

I'm making an endpoint that returns JSON.

{"a":1,"b":1} 

Cake puts an HTML tag in my output.

{"a":1,"b":1} <!-- 0.0964s --> 

Why would it do that to my JSON?

I can put

Configure::write('debug', 0); 

but that suppresses any warnings/errors during development. If something is going wrong, I won't know.

How do I get warnings/errors to print out, but only warnings/errors on requests that have warnings/errors, not a HTML comment for successful responses.

5
  • 2
    After you echo your json string, use die() Commented Mar 14, 2014 at 16:03
  • Are you using the debugkit? Also: You should always mention the exact cakephp version you are using. Commented Mar 14, 2014 at 16:12
  • 2
    @PaulDraper Ohgodwhy isn't right that's not really the proper way of doing it. How are you generating the json? Commented Mar 14, 2014 at 16:16
  • @burzum That's why it wasn't an answer. :) Commented Mar 14, 2014 at 16:30
  • can you tell me how r u generating your json? Commented Mar 14, 2014 at 17:30

2 Answers 2

1

How are you generating the Json?

See http://book.cakephp.org/2.0/en/views/json-and-xml-views.html

You'll have to make sure the app responds as json, this means proper headers, proper layout and proper content. Since you dont show how you generate the Json I assume you don't to it right and the Json is rendered as HTML, probably inside the ajax layout or something else, that's why the string appears there.

Follow the documentation and the string after the json should not appear.

Sign up to request clarification or add additional context in comments.

Comments

0

It might be an error. debug your response.(XHR). using echo or debug(json code here) or vardump(jason..). you need to make sure you are retriving proper json.

To clear you ajax view layout look at my answer here might help on your ajax layout.and also you wont need to add layout for your ajax method.

cakePHP Ajax view

if you are converting array to json you will need to use json_decode. and then in view you have to prase your json

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.