c#根据ip获取地理位置
2021-02-01 21:13
标签:OLE json split com return convert src try amp 前端js 后端接口 c#根据ip获取地理位置 标签:OLE json split com return convert src try amp 原文地址:https://www.cnblogs.com/zhangyouwu/p/11578440.html var shs="";
var shis="";
$(function () {
$.ajax({
type: "POST",
url: "/api/LBCitys/GetAddrs"
})
.success(function (result) {
console.log(result);
if(result!="")
{
shs=result.split("-")[0];
shis=result.split("-")[1];
}
});
[HttpPost]
public string GetAddrs()
{
string ip = GetIpAddress.GetUserIp();
string jsonstr = HttpGet("http://api.map.baidu.com/location/ip?ak=rg3c2fj4QBZwa6v3h1w95Sp9&ip=" + ip);
JObject jo = (JObject)JsonConvert.DeserializeObject(jsonstr);
string json = jo.ToString();//JObject 转json字符串
//return json;
string addr="";
try {
addr = jo["content"]["address_detail"]["province"].ToString() + "-" + jo["content"]["address_detail"]["city"].ToString();
}catch(Exception ex){
addr = "";
}
return addr;
}