Ajax 查询手机号码归属地
在网上找了很多、如果根据网络提供的API直接JS Ajax查询会出问题:拒绝访问
网上说是跨域了、解决办法就是java后台访问这个API地址。下面罗列一些网络上的API地址。
淘宝网
API地址: http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=15850781443
参数:
tel:手机号码
返回:JSON
拍拍
API地址: http://virtual.paipai.com/extinfo/GetMobileProductInfo?mobile=15850781443&amount=10000&callname=getPhoneNumInfoExtCallback
参数:
mobile:手机号码
callname:回调函数
amount:未知(必须)
返回:JSON
财付通
API地址: http://life.tenpay.com/cgi-bin/mobile/MobileQueryAttribution.cgi?chgmobile=15850781443
参数:
chgmobile:手机号码
返回:xml
百付宝
API地址: https://www.baifubao.com/callback?cmd=1059&callback=phone&phone=15850781443
参数:
phone:手机号码
callback:回调函数
cmd:未知(必须)
返回:JSON
115
API地址: http://cz.115.com/?ct=index&ac=get_mobile_local&callback=jsonp1333962541001&mobile=15850781443
参数:
mobile:手机号码
callback:回调函数
返回:JSON
有道
API地址: http://www.youdao.com/smartresult-xml/search.s?jsFlag=true&type=mobile&q=13985046628
参数:
type:mobile(表示查询手机号码)
q:手机号码
返回:JSON
这里用的是有道的API
、下面是代码:
页面代码:
function findPhoneAddres(){
var mobile = $("#usermobil").val();
var urlAction = "<%=path %>/customermanage/listcustomerinfo!findPhoneAddres.action";
$.get(urlAction, {phoneStr:mobile}, function (data){
if(data==''||data==null){
alertMsg.info("找不到您输入的手机号码归属地!");
}else{
var json = eval("("+data+")");
var phoneStr = json.location ;
$("#userAddres").val(phoneStr.split(" ")[1]);
$("#userAddresByPhone").val(phoneStr.split(" ")[1]);
$("#userAddresLabel").html("手机号归属地:"+phoneStr.split(" ")[1])
}
});
}
后台Action方法:
/*
* 手机号码归属地查询地址
*/
private final String urlAddres = "http://www.youdao.com/smartresult-xml/search.s?" +
"jsFlag=true&type=mobile&q=";
/**
* 查询手机号码归属地
* @return
* @throws Exception
*/
public String findPhoneAddres() throws Exception{
String phone = request.getParameter("phoneStr");
String url = urlAddres+phone;
String result = ActionURL.callUrlByGet(url, "GBK");
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.print(result);
out.close();
return null;
}
ActionURL静态类的callUrlByGet方法:
public static String callUrlByGet(String callurl,String charset){
String result = "";
try {
URL url = new URL(callurl);
URLConnection connection = url.openConnection();
connection.connect();
BufferedReader reader = new BufferedReader(new
InputStreamReader(connection.getInputStream(),charset));
String line;
while((line = reader.readLine())!= null){
result += line;
result += "\n";
}
} catch (Exception e) {
e.printStackTrace();
return "";
}
if(result!=null&&!"".equals(result)){
result = result.substring(result.indexOf("{" +
""), (result.indexOf("}")+1) );
}
return result;
}
Test测试方法:
public static void main(String[] args) {
String url = "http://www.youdao.com/smartresult-xml/search.s?" +
"jsFlag=true&type=mobile&q=13985046628";
String result = callUrlByGet(url,"GBK");
System.out.println(result);
}
输出的结果:
{'product':'mobile','phonenum':'13985046628','location':'贵州 贵阳'}