在jQuery中用web api ajax调用填充数组
Fill array with web api ajax call in jQuery
我对jQuery或Javascript有问题。我正在尝试从一个IP阵列在谷歌地图中显示更多的标志。我设法将IP数组传递给了函数,但当我用ajax调用与数组长度一样多的web api时,我遇到了一个问题,结果locations数组为空(未定义);
这是我的代码
function initialize(ipList)
{
var locations = [];
var ips = ipList;
var apiUrl = 'http://freegeoip.net/json/';
// for(var i = 0; i < ips.length; i++)
// {
// jQuery.ajax
// ({
// url: apiUrl + ips[i],
// type: 'POST',
// dataType: 'jsonp',
// success: function(location)
// {
// if(location != null)
// {
// locations.push(location);;
// }
// }
// });
// }
$.each(ips, function(i, x)
{
$.ajax
({
url: apiUrl + x,
type: "POST",
cache: false,
success: function(data)
{
if(data != null)
{
locations[i] = location;
}
}
});
});
var properties =
{
center: new google.maps.LatLng(locations[0].latitude, locations[0].longitude),
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"),properties);
for (var i = 0; i < locations.length; i++)
{
var marker = new google.maps.Marker
({
position:{lat: locations[i].latitude, lng: locations[i].longitude},
animation:google.maps.Animation.BOUNCE,
title: locations[i].city
});
marker.setMap(map);
}
}
你是说要吗
locations[i] = location;
而不是
locations[i] = data;
您可能有上下文、范围的问题。使用回调时,回调函数无法查看初始化调用创建的值。最快的解决方案是在of初始化之外定义位置。
这是完全未经测试的,但还有更多内容。我把map设为全局变量。
var map = null;
function initialize(ipList) {
var ips = ipList;
var apiUrl = 'http://freegeoip.net/json/';
$.each(ips, function (i, x) {
$.ajax
({
url: apiUrl + x,
type: "POST",
cache: false,
success: function (data) {
if (data != null) {
porcessPoint(data);
}
}
});
});
}
function processPoint(datapoint) {
// assuming the first point is used to create the map
if (!map) {
var properties =
{
center: new google.maps.LatLng(datapoint.latitude, datapoint.longitude),
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map"), properties);
}
var marker = new google.maps.Marker
({
position: { lat: datapoint.latitude, lng: datapoint.longitude },
animation: google.maps.Animation.BOUNCE,
title: datapoint.city
});
marker.setMap(map);
}
我希望这对某人有用。这就是我如何更改整个代码:
$(function()
{
$('#details').hide();
$("#btnSubmit").click(function()
{
var ipInput = $("#ipInput").val();
$.ajax
({
type: 'post',
url: 'http://localhost/LocationFromIP/public_html/php/traceroute.php',
data: {param: ipInput},
success: function(data)
{
var ipList = data.match(/'b'd{1,3}'.'d{1,3}'.'d{1,3}'.'d{1,3}'b/g);
for(var i = 0; i < ipList.length; i++)
{
$('#details').append(ipList[i] + '<br />');
}
initialize(ipList);
$('#details').show();
},
error: function (xhr, ajaxOptions, thrownError)
{
alert(xhr.status);
alert(thrownError);
}
});
});
});
function initialize(ipList)
{
var ips = ipList;
var apiUrl = 'http://freegeoip.net/json/';
var map = null;
$.each(ips, function(i, x)
{
$.ajax
({
url: apiUrl + x,
type: "GET",
cache: false,
success: function(location)
{
if (i == 0)
{
var properties =
{
center: new google.maps.LatLng(location.latitude, location.longitude),
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map"),properties);
}
var marker = new google.maps.Marker
({
position:{lat: location.latitude, lng: location.longitude},
animation:google.maps.Animation.BOUNCE,
title: location.city
});
marker.setMap(map);
}
});
});
}
相关文章:
- 第二个Ajax调用已经填充了成功结果
- "堆叠外空间”;同时通过AJAX调用填充jsTree
- Backbone.js在填充集合后调用render
- 设置 AJAX 调用和填充下拉列表
- 在调用方法来填充详细信息网格之前,如何从填充的下拉列表中获取信息
- 调用 HTTP get 方法以使用 angular JS 在下拉列表中填充结果
- 两个异步函数调用填充同一个数组
- 在 Django 中通过 ajax 调用填充选择框的问题
- 在 ajax 调用中填充 JqGrid
- 使用相同的ajax调用填充3个元素
- 通过JS Ajax调用问题填充数据
- Mongoose异步填充错误.未正确调用方法
- 使用两个ajax调用在两个html表中填充数据
- 使用AJAX调用进行表单填充和验证
- 如何将jquery应用于由ajax调用填充的标记
- 为什么在使用 ajax 调用填充表后不使用 jquery 函数
- 从多个mongodb调用填充数组的范围问题
- 尝试用AJAX调用填充选择标记时出现的问题
- 主干-在我的控制器中从一个fetch调用填充多个模型
- 如何使用Jquery Ajax调用填充DropDown