0

I am using this simple code to print an array as a JSON structure.

header('Content-Type: application/json');
echo json_encode($this->data, JSON_PRETTY_PRINT);

I'm using Chrome Version 28.0.1500.95 m. For some odd reason the output is wrapped in a pre tag with a tab character (i.e. \t) at the beginning.

JSON seems to be parsing okay but I still get that tab character when no data is sent. How can I fix this ?

<pre style="word-wrap: break-word; white-space: pre-wrap;"> {
    "title": "Node",
    "items": [
        {
            "label": "Do stuff",
            "icon": "..\/ui\/images\/icons\/16x16\/icon.png",
            "action": "dostuff"
        }
    ]
}</pre>

Edit: Here's the code on the jQuery side:

$.ajax({
    url : "/myproject/getmenu/",
    type : 'GET',
    dataType: "json",
    success : function(data) {
        //alert(JSON.stringify(data,undefined,2));

        if (jQuery.isEmptyObject(data)) {
            return;
        }

        title = data.title;
        items = data.items;

        selected.contextPopup({
            title : title,
            items : items
        });
    }
});
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
James P.
  • 19,313
  • 27
  • 97
  • 155
  • When are you using `jQuery.isEmptyObject`? – Explosion Pills Aug 23 '13 at 20:55
  • 1
    You need to show your JS code, too. – ThiefMaster Aug 23 '13 at 20:57
  • I've added the jQuery side. – James P. Aug 23 '13 at 23:02
  • @ExplosionPills I wanted to use it to test if no data is sent back. Not sure if it's the appropriate form. – James P. Aug 23 '13 at 23:18
  • where does `$this->data` come from? Does it come from a file, database, etc.? Is it stored with the `
    ` tag and tab, or when loaded into that variable is the `
    ` tag added?
    – Sᴀᴍ Onᴇᴌᴀ Aug 16 '17 at 07:11
  • @SamOnela this was years ago but would have been JSON output from a PHP script. From what I remeber, it was peculiar behaviour specific to Chrome. – James P. Aug 20 '17 at 17:49
  • Is it still an open issue? If I were you I would edit the question to add context of the current status... as you can see below, users are still answering it. If it is still an open issue, adding more background information about the origin of `$this->data` would be helpful – Sᴀᴍ Onᴇᴌᴀ Aug 20 '17 at 18:52
  • I was not aware that you could close a question on SE. It's from 2013 and I've moved on to other things since but the issue may still exist for other people. – James P. Aug 20 '17 at 19:16

2 Answers2

2

You need to remove whatever code adds the <pre> tag. This causes your response to be invalid JSON (the whitespace to prettyprint it is not a problem though) and thus makes jQuery fail when parsing it.

I couldn't see anything in the PHP docs about the JSON response being wrapped in <pre> but you could surely try it without the flag. I'd also make sure to check if the tag is actually in your response. If you use view-source and have a JSON-pretty-printing browser extension installed it might be added by that extension and not be in the actual JSON handled by your JavaScript code.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • JSON is being parsed fine by jQuery but that '\t' is being passed when there's no response. There is no apparent code on the PHP side that is causing the `pre` to be printed. If you search on google for that pre tag you will see topics posted on the subject. There is no extension along those lines either. That's why I'm baffled and asking why this could be happening. – James P. Aug 23 '13 at 23:11
  • See this topic for example: http://stackoverflow.com/questions/3002109/what-do-browsers-want-for-the-content-type-header-on-json-ajax-responses – James P. Aug 23 '13 at 23:12
1

hmmm,I was searching for an correct answer my self but none of them worked for. but I tried to do as following and it worked for me ...

1- I set the content type to application/json

2- I used the die(json_encode($this->data)) instead of echo json_encode($this->data)

hope it will work for you although I guess its a bit too late :D forgive me for answering an old question I recently run into same issue myself

N-Alpr
  • 336
  • 2
  • 11