【发布时间】:2013-12-16 01:55:23
【问题描述】:
这是我的代码:
// 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 的美妙世界!你不能那样做。
-
乍一看,这似乎是一个与 (a)sync 调用有关的问题,但很难从给定的代码中分辨出来。
-
...所以设置一个 $.ajax({/* blah blah */,success: function(data){data.totalVotes = voteCount; localStorage..... }})跨度>
-
感谢@SLaks,但它是 async:false,所以这肯定不是异步问题?
-
那就错了。
async: false冻结浏览器;不要那样做。
标签: javascript json stringify