在 Node-express 服务器中,我获得了大量 IP 地址,我想在客户端的 map 上将其可视化。
由于数据集很大,我认为最好向客户端返回一个包含经度和纬度数组的json。
问题是,从 IP 地址到地理位置,我需要向 api 发送 ajax 请求。我可以在服务器中执行此操作吗?
$.ajax({
type: "GET",
url: 'http://api/'+ip,
dataType: "jsonp",
success: function (res) {
//do something;
}
});
谢谢。
在服务器端,你会得到类似的东西:
app.get('/api/:ip',function(req, res){
var ip = req.params.ip;
geolocalizeIp(ip,function(latlng){ //you have to write this function
res.json(latlng);
});
});
客户端将使用以下方式获取它:
$.ajax({
type: "GET",
url: 'http://yourserver/api/'+ip,
dataType: "json",
success: function (res) {
//res is yourArray, do stuff with it here;
}
});
我是一名优秀的程序员,十分优秀!