这是我的代码:JSON.stringify丢失/缺少的字段
// get from ajax
$.ajax({
'async': false,
'global': false,
'url': url,
'dataType': "json",
'success': function (d) {
data = d;
// code here to define & calculate voteCount
data.votes.totalVotes = voteCount;
localStorage.setItem(url, data);
}
, 'error': function (msg) {
throw new Error("Error retrieving data from " + url);
}
});
这一行后:
data.votes.totalVotes = voteCount;
...在控制台,如果我输入:data.votes.totalVotes
,我得到的值我期待。
但是,如果我键入:JSON.stringify(data)
,totalVotes
属性不存在。
任何想法为什么不,或如何解决它?
**注意:编辑是因为据我所知,这不是异步问题,行为也存在于成功回调中。
欢迎来到** async **的美妙世界!你不能那样做。 – SLaks
乍一看,它看起来像一个问题,涉及(a)同步呼叫,但很难从给定的代码中分辨出来。 – Sirko
...所以设置$ .ajax({/ *等等等等,/成功:函数(数据){data.totalVotes = voteCount; localStorage .....}}) –