我有JSON文件input.json:
{
"consumner_key": {
"display_name": "CONSUMER-KEY:",
"name":"consumer_key",
"format": "string",
"type": "textbox",
"isMandatory": "true"
},
"secret_key": {
"display_name": "CONSUMER-SECRET:",
"name":"consumer_secret",
"format": "string",
"type": "textbox",
"isMandatory": "true"
}
}我使用$.getJSON()获取JSON文件并对其进行解析:
$.getJSON('input.json',function jsonData(Data)
{
$.each(Data, function(m,field)
{
console.log(m);
$('#tabs-4 social').append('<input></input>').attr({type:'text',name:this.consumner_key+''+this.name});
$('#tabs-4 social').append('<input></input>').attr({type:'text',name:this.secret_key+''+this.name});
});
});当我运行它时,我无法在我的标签中查看输入框。请指出我的错误所在。
发布于 2011-08-24 18:22:22
我终于设法清除了tomcat的web.xml中的所有errors.Specifying MIME类型是不够的,我不得不在我的js file.The中手动覆盖它下面的代码删除了错误“不是很好的格式”
$.ajaxSetup({beforeSend: function(xhr){
if (xhr.overrideMimeType)
{
xhr.overrideMimeType("application/json");}
}
});另外,我在jsp和javascript文件中都引用了json文件,这导致了无效标签错误。
发布于 2011-08-22 16:00:03
你说你有文件input.js,但是在代码中你有input.json -这会是一个原因吗?
更新
您还可以为您的函数命名- jsonData。据我所知,您不应该命名内联javascript函数,它将不会编译。只管去做
$.getJSON('input.json',function (Data) {
$.each(Data, function(m,field) {
console.log(m);
$('#tabs-4 social').append('<input></input>').attr({type:'text',name:this.consumner_key+''+this.name});
$('#tabs-4 social').append('<input></input>').attr({type:'text',name:this.secret_key+''+this.name});
});
});更新2
此外,您可以使用this.consumner_key+''+this.name和this.secret_key+''+this.name,而consumner_key和name是不同级别的属性。我相信,each中的this应该表示json对象中的每个单个子对象。因此它将具有name属性,但不具有consumner_key属性。我可能错了,但无论如何this不能同时拥有这两个属性。
https://stackoverflow.com/questions/7144553
复制相似问题