借鉴:https://blog.csdn.net/enweitech/article/details/52084346
搜狐IP地址查询接口(默认GBK):http://pv.sohu.com/cityjson
搜狐IP地址查询接口(可设置编码):http://pv.sohu.com/cityjson?ie=utf-8
接口返回的数据是:
var returnCitySN = {"cip": "119.137.53.105", "cid": "440306", "cname": "广东省深圳市宝安区"};
下面列举三种调用方式:
js调用:
<script src="http://pv.sohu.com/cityjson?ie=utf-8 "></script> //引用JavaScript地址接口
<script type="text/javascript"> document.write(returnCitySN["cip"]+','+returnCitySN["cname"]) </script> //打印截取指定的字段值
jQuery的getScript()调用:
$.getScript('http://pv.sohu.com/cityjson?ie=utf-8')
.done(function(script, textStatus,jqxhr) {
console.log(returnCitySN.cip);//xx.xx.xx.xx
console.log(textStatus);//success
console.log(jqxhr.status);//200
})
.fail(function () {
console.log('访问错误');
});
ajax调用(注意dataType类型是script):
$.ajax({
url: 'http://pv.sohu.com/cityjson?ie=utf-8',
dataType: "script",
method: 'GET',
}).done(function () {
alert(returnCitySN.cip);//xx.xx.xx.xx
});