[英]getScript or ajax
I want to get Javascript data in another page and use it in an existing javascript. 我想在另一个页面中获取Javascript数据,并在现有的JavaScript中使用它。
Let me explain it with the codes. 让我用代码解释一下。
I want to get this data from that other page 我想从其他页面获取此数据
<script type="text/javascript">
var data = {
"users": [
{
"latitude": "48.405163",
"longitude": "2.684659"
},
{
"latitude": "43.7347242529278",
"longitude": "7.42198348045349"
}
]
};
</script>
I tried to get it it using this code 我尝试使用此代码来获取它
$(function MapData() {
$.getScript({
type: "GET",
url: "default.cs.asp?Process=ViewCheckinMap",
success: function(data) {
$("#content").append(data);
},
error: function (data) {
$("#content").append(data);
}
});
});
In order to use data inside the data variable it in this code 为了在数据变量中使用数据,在此代码中
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);
But I am getting data is not defined error. 但是我正在获取数据未定义错误。
I also tried getting data from the other page using 我也尝试使用其他页面获取数据
$.getScript('default.cs.asp?Process=ViewCheckinMap');
But I got the same error. 但是我遇到了同样的错误。
How can I fix this? 我怎样才能解决这个问题? Many thanks! 非常感谢!
When you're loading javascript with $.getScript()
, you should not include <script>
tags in the file you're loading. 使用$.getScript()
加载javascript时,不应在要加载的文件中包含<script>
标记。 Also, you need to run your initialize()
function after you've finished loading the file: 同样,在完成文件加载后 ,您需要运行initialize()
函数:
$.getScript('default.cs.asp?Process=ViewCheckinMap', initialize)
data is available only for the call back function of the getScript call. 数据仅可用于getScript调用的回调函数。 If you want to use it outside, store it in a global variable and use it. 如果要在外部使用它,请将其存储在全局变量中并使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.