Tab and pre wrapped around JSON output in Chrome
Asked Answered
G

2

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
        });
    }
});
Galeiform answered 23/8, 2013 at 20:52 Comment(8)
When are you using jQuery.isEmptyObject?Rakel
You need to show your JS code, too.Villareal
I've added the jQuery side.Galeiform
@ExplosionPills I wanted to use it to test if no data is sent back. Not sure if it's the appropriate form.Galeiform
where does $this->data come from? Does it come from a file, database, etc.? Is it stored with the <pre> tag and tab, or when loaded into that variable is the <pre> tag added?Spiccato
@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.Galeiform
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 helpfulSpiccato
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.Galeiform
V
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.

Villareal answered 23/8, 2013 at 20:55 Comment(2)
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.Galeiform
See this topic for example: #3002609Galeiform
E
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

Eaten answered 16/8, 2017 at 6:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.