我正在使用 jquery ui 选项卡和 ajax。
Ajax 将面对这样的 JSON 内容。
[
{
"title" :"a note",
"type" :"text",
"content" :"MY FIRST NOTE!"
},
{
"title" :"two note",
"type" :"text",
"content" :"MY FIRST NOTE <b>if html works<\/b> i should pay attention to this!"
}
]
我正在使用此代码:
$(function() {
$("#tabs").tabs({
cache : false,
ajaxOptions : {
cache : false,
dataType : 'json',
dataFilter : function(result) {
var data = $.parseJSON(result);
return data;
},
error : function(xhr, status, index, anchor) {
$(" anchor.hash ").html("Couldn't load this tab. We'll try to fix this as soon as possible. " + "If this wouldn't be a demo.");
}
}
});
});
(我见过这个问题 Loading JSON-encoded AJAX content into jQuery UI tabs )
JSON 文件(由 php 生成)已正确加载,我已使用 JSONLint 对其进行了验证,但选项卡保持白色且内容未加载,您能帮助我吗?
这是我第一次使用 JSON 和 Ajax,所以如果我犯了一些愚蠢的错误,请原谅我
编辑:json 内容以内容类型 = application/json 发送,删除显示 json 的内容类型,但我想使用 jquery 解析 json 文件,这可能吗?
最佳答案
我认为你不应该调用 $.parseJSON(result);
因为你指定了 dataType : 'json'
(看看我对这个问题的回答 Why is 'jQuery.parseJSON' not necessary? )这样 jQuery 就会为您解析响应。查看另一个示例,您还应该返回
dataFilter : function(result) {
var data = $.parseJSON(result);
return data.content;
},
编辑 - 让 dataType: 'json'
这应该没问题
dataFilter : function(result) {
return result.content;
},
关于jQuery UI 选项卡 : JSON not displaying,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8872530/