我在网页上有一个带有标记的 Google map ,上面附有点击监听器。
问题:在 localhost 上,当我点击 map 标记时,一切正常。但是,在上传到服务器并尝试 live 版本后,单击标记会导致错误 Uncaught TypeError: Cannot read property 'listing_id' of undefined
。
console.log
都显示 json
和 i
已定义...
Console.log 输出
[ Object , Object , Object , Object , Object , Object , Object , Object , Object , Object , Object , Object , Object , Object , Object , Object , Object , Object ]
i: 48
Uncaught TypeError: Cannot read property 'listing_id' of undefined
为什么会这样?
JS代码
$.getJSON(......., function(json) {
for(var i = 0; i < json.length; i++) {
(function(i) {
google.maps.event.addListener(markers[json[i].listing_id], 'click', function() {
console.log('json: ' + json);
console.log('i: ' + i);
console.log('json[i].listing_id :' + json[i].listing_id);
// Mark currently opened marker
markers[json[i].listing_id].setIcon(base_url + 'images/template/markers/listing_black.png');
//...
});
})(i);
}
});
最佳答案
看起来你没有将数据传递给回调函数。
尝试将第一行改为:
$.getJSON(......., function(json) {
关于javascript - 在实时服务器上未定义,但在本地主机上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10064202/