这让我疯了!
我从服务器上获得了一些JSON:
{"id262":{"done":null,"status":null,"verfall":null,"id":262,"bid":20044,"art":"owner","uid":"demo02","aktion":null,"termin_datum":null,"docid":null,"gruppenid":null,"news":"newsstring","datum":"11.06.2010","header":"headerstring","for_uid":"demo01"},
"id263":{"done":null,"status":"pending","verfall":null,"bid":20044,"id":263,"uid":"demo02","art":"foo","aktion":"dosomething","termin_datum":"11.06.2010","docid":null,"gruppenid":null,"datum":"11.06.2010","news":"newsstring","for_uid":"demo01","header":"headerstring"},
"id261":{"done":null,"status":null,"verfall":null,"id":261,"bid":20044,"art":"termin","uid":"demo02","aktion":null,"termin_datum":"25.06.2010","docid":null,"gruppenid":null,"news":"newsstring","datum":"11.06.2010","header":"headerstring","for_uid":null}}
这就是我的JS的样子:
var user = 'demo02';
new Ajax.Request('myscript.pl?someparameter=value', { method:'get',
onSuccess: function(transport){
var db_json = transport.responseText.evalJSON(),
propCount = 0,
someArray1 = [],
someArray2 = [],
otherArray = [];
//JSON DEBUG
console.log('validated string:');
console.log(transport.responseText.evalJSON(true));
for(var prop in db_json) {
propCount++;
if ( (db_json[prop].art == 'foo') && (db_json[prop].for_uid == user) ) {
someArray1.push(db_json[prop]);
} else if( (db_json[prop].art == 'foo') && (db_json[prop].uid == user) ) {
someArray2.push(db_json[prop]);
} else if( db_json[prop].art == 'log' ) {
otherArray.push(db_json[prop]);
}
}
if(someArray1.length>0) {
someArray1.map(function(el){
$('someArray1target').innerHTML += el.done;
//do more stuff
});
}
if(someArray2.length>0) {
someArray2.map(function(el){
$('someArray2target').innerHTML += el.done;
//do more stuff
});
}
});
有时,它完美无缺。
有时候,我得到了我的JSON字符串(它出现在Firebug的“回答”-tab中),但它不会在console-log()中记录JSON。我没有收到任何错误,javascript仍在运行。
下次重新加载后,它可能会起作用,但可能不会。
我无法想象为什么有时会发生这种情况!
答案 0 :(得分:1)
您正在调用evalJSON两次,实际上每次都使用不同的参数 通常情况下,我不希望这有任何副作用,实际上这种方法的原型文档没有提到任何副作用。但是,我记得早期版本的firebug已经以奇怪的方式操纵XMLHttpRequest(为了捕获进出的数据),所以今天可能仍然有用。
请尝试将日志语句更改为:
console.log(db_json);
答案 1 :(得分:0)
我找到了答案。这让我想要砰的一声。我的$('someArray1target')div有时候还没有加载。
我非常专注于在我的JSON中找到一些奇怪的东西,而不是寻找更明显的“标准”错误。