我想根据他的IP地址的国家/地区重定向我的Xpage的访问者。因此,当访问者来自法国时,他应该被重定向到france.xsp,德国到Germany.xsp等。我认为这应该是可能的:地理 ip API
我不知道如何实现它,或者也许有人有更好的解决方案?
编辑:由于此页面已经加载了 Jquery,因此我从 Alexandro 那里获取了解决方案,我已将其放入客户端加载事件中
有一个免费的网站可以做到这一点,http://freegeoip.net/
您可以在 http://freegeoip.net/json/执行 JSON 请求,也可以使用返回的数据,例如使用 jQuery 在客户端执行此操作:
jQuery.getJSON('http://freegeoip.net/json/', function(location) {
// If the visitor is browsing from Canada.
if (location.country_code == 'CA') {
// Redirect him to the canadian store.
window.location.href = 'http://shop-in-canada.com';
}
});
有关getJSON方法的更多信息:http://api.jquery.com/jQuery.getJSON/
希望对:)有所帮助
Alesanco 回答中的网站是一个很好的资源,但如果你想在不使用 jQuery 库的情况下做到这一点,以下是如何在 Dojo 中做到这一点,而无需向 XPage 添加任何其他内容。
require(['dojo/_base/xhr'], function(xhr){
xhr.get({
url:"http://freegeoip.net/json/", handleAs:"json",
load: function(data){
// data is a JavaScript object. The content of foo.php
// was passed through dojo.fromJson
alert(data.country_code); //Returns the country code. Use this to drive your logic.
}
});
});
如果您使用的是 Domino 9(在 Domino 前面安装了 IBM Http Server),或者如果您在 Domino 前面使用 Apache 或 Nginx 等 Web 服务器,则可以使用以下模块:
- https://github.com/maxmind/geoip-api-mod_geoip2 - Apache 2 GeoIP模块
- http://wiki.nginx.org/HttpGeoipModule - Nginx GeoIP模块
这样,您将不依赖于硬编码检查