调用 Document.ready 外部的 AJAX 函数

calling ajax function that is outside document.ready

本文关键字:AJAX 函数 外部 Document ready 调用      更新时间:2023-09-26

我正在尝试填充谷歌地图。这是我的一些代码。为什么这个 ajax 请求不起作用?当我将其全部作为匿名函数粘贴在 document.ready() 中时,它工作正常,但我想重用这段代码,所以我需要能够调用它。

$(document).ready(function(){
    var map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: 49.105, lng: -97.568},
        zoom: 4
      });
getTornadoes("test", "test", "yes");            
});
// Get the tornado JSON file
    function getTornadoes (test, test2, test3) {//These are not my real parameters
        $.getJSON('water_pollutants.php', function(data){
            $.each(data.features, function(index, feature){
                var longitude = feature.properties.LONGITUDE;
                var latitude = feature.properties.LATITUDE;
                ...

我收到错误InvalidValueError:setMap:不是Map的实例;也不是StreetViewPanorama的实例。但是,我不认为这是谷歌地图的问题。过去,在尝试引用在 document.ready() 外部声明的 ajax 函数时,我曾遇到过类似的问题。

我也使用谷歌地图api,这里有一个建议:

改变

$(document).ready(function(){
      var map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: 49.105, lng: -97.568},
        zoom: 4
      });

自:

var map;
$(document).ready(function(){
      map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: 49.105, lng: -97.568},
        zoom: 4
      });