在实时服务器上未定义,但在本地主机上工作

Undefined on live server, but WORKS on localhost

本文关键字:主机 工作 实时 服务器 未定义      更新时间:2023-09-26

我在网页上有一个谷歌地图,上面有标记,上面附有点击监听器。

问题:在本地主机上,当我单击地图标记时,一切都按预期工作。但是,在上传到服务器并尝试实时版本后,单击标记会导致错误Uncaught TypeError: Cannot read property 'listing_id' of undefined

这两个console.log都表明jsoni是定义的......

控制台.log输出

[ 对象 , 对象 , 对象 , 对象 , 对象

, 对象 , 对象 , 对象 , 对象 , 对象 , 对象 , 对象 , 对象 , 对象 , 对象 , 对象 , 对象 , 对象 ]

一: 48

捕获的类型错误:无法读取未定义的属性"listing_id"

为什么会这样?

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) {