gpt4 book ai didi

ajax - 如何从node-express发送AJAX请求?

转载 作者:太空宇宙 更新时间:2023-11-04 01:02:01 25 4
gpt4 key购买 nike

在 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;
}
});

关于ajax - 如何从node-express发送AJAX请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26209262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com