jsonp跨域請求數(shù)據(jù)實現(xiàn)手機號碼查詢實例分析
本文實例講述了jsonp跨域請求數(shù)據(jù)實現(xiàn)手機號碼查詢的方法。分享給大家供大家參考,具體如下:
前言
網(wǎng)上有很多開放的api,我們在本地通過ajax獲取數(shù)據(jù)時,總會碰到一個問題,那就是跨域!如果不借助php等,僅僅通過js怎么解決跨域的問題呢?或許jsonp是個不錯的選擇。
知識準備
之前一篇《說說JSON和JSONP 也許你會豁然開朗》對jsonp已經(jīng)介紹的很詳細了,這里就不多介紹了,直接上實例吧
代碼:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<meta name="author" content="@my_coder">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<title> 手機號查詢 </title>
<style type="text/css">
html{color:#000;background:#fff;}
body,ul,li,input,h1,button,p{padding:0;margin:0;}
li{list-style:none;}
html{background:#F6F8FC;overflow:hidden;}
.outer{margin:0 auto;width:280px;position:relative;}
h1{font-size:20px;text-align:center;border-bottom:1px dotted #A3C4DB;padding:10px 0;}
p{font-size:14px;padding:14px 0 10px;}
input[type="text"]{width:200px;height:30px;font-size:18px;}
.button{display:inline-block;width:60px;font-size:16px;text-align:center;line-height:34px;background:linear-gradient(#fff,#ccc);border:1px solid #004;border-radius:3px;cursor:pointer;}
ul{padding-top:26px;}
li {font-size:18px;line-height:30px;}
.error{position:absolute;left:4px;top:80px;color:red;font-size:14px;display:none;}
</style>
</head>
<body>
<h1>手機號碼歸屬地查詢</h1>
<div class="outer">
<p>請輸入手機號碼</p>
<input type="text" >
<span class="button">查詢</span>
<span class="error">號碼有誤 或 無數(shù)據(jù)</span>
<ul>
<li class="num">手機號碼: <span></span></li>
<li class="province">歸屬省份: <span></span></li>
<li class="operators">運 營 商: <span></span></li>
</ul>
</div>
<script type="text/javascript" src="jquery-1.8.0.min.js" ></script>
<script>
var tel;
var ajax=function(){
//淘寶接口
$.ajax({
type: "get",
url: 'http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel='+tel,
dataType: "jsonp",
jsonp: "callback",
success: function(data){
console.log(data);
$('.error').css('display','none');
var province = data.province,
operators = data.catName,
num = data.telString;
$('.num span').html(num);
$('.province span').html(province);
$('.operators span').html(operators);
},
error:function (){
$('li span').html('');
$('.error').css('display','block');
}
});
}
var reg = /^(13|15|18)[0-9]{9}$/;
//點擊查詢
$('.button').click(function(){
tel=$('input[type=text]').val();
if(tel){
if(reg.test(tel)){
ajax();
}else{
$('li span').html('');
$('.error').css('display','block');
}
}
});
//鍵盤事件
$(window).keydown(function(event){
tel=$('input[type=text]').val();
if(event.keyCode==13) {
if(tel){
if(reg.test(tel)){
ajax();
}else{
$('li span').html('');
$('.error').css('display','block');
}
}
}
});
</script>
</body>
</html>
希望本文所述對大家jQuery程序設(shè)計有所幫助。
- jQuery使用JSONP實現(xiàn)跨域獲取數(shù)據(jù)的三種方法詳解
- 通過jsonp獲取json數(shù)據(jù)實現(xiàn)AJAX跨域請求
- JavaScript用JSONP跨域請求數(shù)據(jù)實例詳解
- 基于Jquery的跨域傳輸數(shù)據(jù)(JSONP)
- AJAX跨域請求之JSONP獲取JSON數(shù)據(jù)
- 使用jsonp實現(xiàn)跨域獲取數(shù)據(jù)實例講解
- AngularJS實現(xiàn)的JSONP跨域訪問數(shù)據(jù)傳輸功能詳解
- Ajax的jsonp方式跨域獲取數(shù)據(jù)的簡單實例
- AJAX跨域請求JSONP獲取JSON數(shù)據(jù)的實例代碼
- jsonp跨域獲取數(shù)據(jù)的基礎(chǔ)教程
相關(guān)文章
JQuery查找子元素find()和遍歷集合each的方法總結(jié)
下面小編就為大家?guī)硪黄狫Query查找子元素find()和遍歷集合each的方法總結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03
jquery.messager.js插件導致頁面抖動的解決方法
這盤文章介紹了jquery.messager.js插件導致頁面抖動的解決方法,有需要的朋友可以參考一下2013-07-07
關(guān)于jquery append() html時的小問題的解決方法
關(guān)于jquery append() html時的小問題,碰到類似問題的朋友可以參考下。2010-12-12
Jquery 選中表格一列并對表格排序?qū)崿F(xiàn)原理
該實現(xiàn)的主要思想是:獲取鼠標點擊的表頭單元格的列號,遍歷數(shù)據(jù)行,獲取每個<tr>中的html,同時獲取每個<tr>標簽下對應(yīng)獲取到的列號的<td>標簽中的內(nèi)容,并取得<th>標簽的type屬性值,需要的朋友可以了解下2012-12-12
BootStrap表單驗證中的非Submit類型按鈕點擊時觸發(fā)驗證的坑
這篇文章主要介紹了BootStrap表單驗證中的非Submit類型按鈕點擊時觸發(fā)驗證的坑,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09
jQuery插件HighCharts實現(xiàn)的2D面積圖效果示例【附demo源碼下載】
這篇文章主要介紹了jQuery插件HighCharts實現(xiàn)的2D面積圖效果,結(jié)合完整實例形式分析了jQuery插件HighCharts繪制2D面積圖的相關(guān)步驟與屬性設(shè)置技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-03-03

