[英]JSON VALUE AS QUERY STRING
I'm using Maxmind.Com's GeoIP2 (Omni) Webservice on my Drupal 7 website to pull geographic data based on my visitor's IP. 我在Drupal 7网站上使用Maxmind.Com的GeoIP2(Omni)Web服务来根据访问者的IP提取地理数据。
I am able to get a JSON document by using this code: 我可以使用以下代码获取JSON文档:
<script type="text/javascript" src="//js.maxmind.com/js/apis/geoip2/v2.0/geoip2.js">
</script>
<script type="text/javascript">
var onSuccess = function(location){
console.log(
"Lookup successful:\n\n"
+ JSON.stringify(location.city.names.en, undefined, 4)
);
};
var onError = function(error){
alert(
"Error:\n\n"
+ JSON.stringify(error, undefined, 4)
);
};
geoip2.omni(onSuccess, onError);
</script>
One of the returned values is the visitor's city name. 返回的值之一是访客的城市名称。 How can I use the returned value as part of a query string?
如何将返回值用作查询字符串的一部分?
For example, if 'city.names.en' = 'Detroit'
, how could I use "Detroit" as a key to retrieve data (eg Local phone number) from another document, table, etc? 例如,如果
'city.names.en' = 'Detroit'
,我如何使用“底特律”作为键来从另一个文档,表等中检索数据(例如本地电话号码)?
The end goal is to dynamically insert a "local" phone number into the "contact us" section based on the visitor's location. 最终目标是根据访问者的位置动态地将“本地”电话号码插入“与我们联系”部分。
Typically you'll format a query string like this: 通常,您将格式化查询字符串,如下所示:
http://mydomain.com/route?parameterName=value
For your example it may look like this: 对于您的示例,它可能看起来像这样:
http://mydomain.com/phoneNumbers/phoneNumber?city=Detroit
There can be two approach to achieve what you want to do. 可以采用两种方法来实现您想要的工作。
<span id="contact"></span>
<script type="text/javascript"> $(document).ready(function() { geoip2.omni(function(data) { $.ajax({ type: "GET", url: "your/url/to/contact/getter/service", data: { city : data.city.names.en }, success: function(contact) { // update your contact information $("#contact").text(contact); } } }, function(data) { debugger; }); }); </script>
Obviously, you need a resource that you can request to provide the contact information given the city name. 显然,您需要一种资源,可以请求提供给定城市名称的联系信息。
<span id="contact"></span>
<script type="text/javascript"> var contactMap = { NewYork : "123123", London : "9797987" } $(document).ready(function() { geoip2.omni(function(data) { // update your contact information $("#contact").text(contactMap[data.city.names.en]); }, function(data) { debugger; }); }); </script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.