在Chrome中,Tab和Pre包裹着JSON输出 [英] Tab and pre wrapped around JSON output in Chrome
问题描述
header('Content-Type:application)使用这个简单的代码将数组打印为JSON结构。 / JSON');
echo json_encode($ this-> data,JSON_PRETTY_PRINT);
我使用Chrome版本28.0.1500.95米。出于某种奇怪的原因,输出在开头用一个制表符(即 \ t
)包装在一个pre标签中。
< pre style =word-wrap:break-word; white-space:pre-包裹;> {
title:Node,
items:[
{
label:做东西,
icon:。 。\ / ui \ / images \ / icons \ / 16x16 \ / icon.png,
action:dostuff
}
]
}< /预>
编辑:这是jQuery端的代码:
$。ajax({
url:/ myproject / getmenu /,
类型:'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({ b $ b title:title,
items:items
});
}
});
$ c>< pre> 标签。这会导致你的响应是无效的JSON(虽然这个问题没有问题),因此jQuery在解析它时失败了。
我什么都看不到在关于将JSON响应封装在< pre>
中的PHP文档中,但是您一定可以在没有标志的情况下尝试它。我还要确保检查标签是否在您的回复中。如果你使用view-source并且安装了一个JSON漂亮的打印浏览器扩展,它可能会被该扩展添加,而不是被你的JavaScript代码处理的实际JSON。
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
});
}
});
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.
这篇关于在Chrome中,Tab和Pre包裹着JSON输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!