簡體   English   中英

getScript或Ajax

[英]getScript or ajax

我想在另一個頁面中獲取Javascript數據,並在現有的JavaScript中使用它。

讓我用代碼解釋一下。

我想從其他頁面獲取此數據

   <script type="text/javascript">
      var data = {
  "users": [
    {
      "latitude": "48.405163",
      "longitude": "2.684659"
    },
    {
      "latitude": "43.7347242529278",
      "longitude": "7.42198348045349"
    }
  ]
};
    </script> 

我嘗試使用此代碼來獲取它

    $(function MapData() {
        $.getScript({
            type: "GET",
            url: "default.cs.asp?Process=ViewCheckinMap",
            success: function(data) {
                $("#content").append(data);
            },
            error: function (data) {
                $("#content").append(data);
            }
        });
    });

為了在數據變量中使用數據,在此代碼中

  function initialize() {
    var center = new google.maps.LatLng(48.404840395764175, 2.6845264434814453);

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 3,
      center: center,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var markers = [];
    for (var i = 0; i < data.users.length; i++) {
      var location = data.users[i];
      var latLng = new google.maps.LatLng(location.latitude,
          location.longitude);
      var marker = new google.maps.Marker({
        position: latLng
      });
      markers.push(marker);
    }
    var markerCluster = new MarkerClusterer(map, markers);
  }
  google.maps.event.addDomListener(window, 'load', initialize); 

但是我正在獲取數據未定義錯誤。

我也嘗試使用其他頁面獲取數據

$.getScript('default.cs.asp?Process=ViewCheckinMap');

但是我遇到了同樣的錯誤。

我怎樣才能解決這個問題? 非常感謝!

使用$.getScript()加載javascript時,不應在要加載的文件中包含<script>標記。 同樣,在完成文件加載 ,您需要運行initialize()函數:

$.getScript('default.cs.asp?Process=ViewCheckinMap', initialize)

數據僅可用於getScript調用的回調函數。 如果要在外部使用它,請將其存儲在全局變量中並使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM