jsonp之jQuery.getScript()方法的簡(jiǎn)單使用

<!DOCTYPE HTML>
<html lang="en-US">
<head>
	<meta charset="UTF-8">
	<title>getScript簡(jiǎn)單學(xué)習(xí)</title>
	<script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.8.3/jquery.min.js"></script>
	<script type="text/javascript">
		/*
			參考:http://www.365mini.com/page/jquery_getscript.htm
				  http://www.w3school.com.cn/jquery/ajax_getscript.asp
				  http://www.css88.com/jqapi-1.9/jQuery.getScript/
				  http://www.111cn.net/wy/jquery/83696.htm
				  
			
			1.jQuery.getScript()函數(shù)用于通過(guò)HTTP GET形式加載
			  Javascript文件并運(yùn)行它。
			
			2.該函數(shù)用于動(dòng)態(tài)加載JS文件,并在全局作用域下執(zhí)行文件中的JS代碼。
			
			3.該函數(shù)可以加載跨域的JS文件。請(qǐng)注意,該函數(shù)是通過(guò)異步加載數(shù)據(jù)的。
			
			4.該函數(shù)屬于全局的jQuery對(duì)象。
			
			5.注意:如果多次加載相同URL的js文件,即使服務(wù)器對(duì)js文件啟用了緩存,
			  在第二次及以后加載該js文件時(shí),jQuery.getScript()仍然不會(huì)緩存。
			  因?yàn)樵摵瘮?shù)會(huì)在js文件的URL后面添加一個(gè)時(shí)間戳參數(shù)后綴,
			  從而避免瀏覽器獲取緩存的js文件。
			
			6.請(qǐng)注意不要直接在jQuery.getScript()執(zhí)行后直接調(diào)用該js文件中
			  的變量或函數(shù),因?yàn)閖Query.getScript()是異步加載的,
			  在你訪問(wèn)其中的某個(gè)變量或函數(shù)時(shí),可能該js文件尚未完全加載完畢。
			  建議你最好在success回調(diào)函數(shù)中處理,
			  或者你能夠確認(rèn)此時(shí)該js文件已經(jīng)加載完畢。
		*/
		
		$.getScript("http://js.tv.itc.cn/kao.js",function(){
			$("#go").click(function(){
				alert("OK");
			});
		});
		
		//回到函數(shù)的三個(gè)返回值,第一個(gè)返回值,第二個(gè)是狀態(tài),第三個(gè)是狀態(tài)碼
		$.getScript("http://js.tv.itc.cn/kao.js",function(data,textStatus,jqxhr){
			console.log(data); //undefiend  
			console.log(textStatus); //success
			console.log(jqxhr.status); // 200
			console.log("Load was performed!");
		});
		
		//處理錯(cuò)誤情況
		$.getScript("http://js.tv.itc.cn/dicta.js")
		 .done(function(data,textStatus,jqxhr){ //也可以只要前兩個(gè)參數(shù)或前一個(gè)
			console.log("Load was performed1--------------!");
			console.log(data); //undefined
			console.log(textStatus); //success
			console.log(jqxhr.status); // 200
			console.log("Load was performed1--------------!");
		 })
		 .fail(function(jqxhr, settings, exception){
			console.log("***********************************!");
			console.log("***********************************!");
		 });
		
	
		$(document).ready(function(){
		
			
		
		});
	</script>
</head>
<body>
	<button id="go">Run</button>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
	<title>JSONP之getScript的用法</title>
	<script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.8.3/jquery.min.js"></script>
	<script type="text/javascript">
		/*
			我們?cè)谶@邊文章中分析了JSONP:http://hanchaohan.blog.51cto.com/2996417/1291927
			
			下面我分析一下使用匿名JSONP的使用方法。
			
			比如:http://store.tv.sohu.com/web/stpc/saveInformation.do
			返回的是:var result={"status":-2} 。
			接口又不支持JSONP,我們此時(shí)使用匿名JSONP格式就比較好!
			
		*/
		
		$(document).ready(function(){
			var t=encodeURIComponent($("#form_name").val()),
				a=encodeURIComponent("北京"),
				f=encodeURIComponent("北京"),
				r=encodeURIComponent("hanchaohan@sohu-inc.com"),
				l=encodeURIComponent("demo"),
				c=encodeURIComponent("13581746914");
				
				$.getScript("http://store.tv.sohu.com/web/stpc/saveInformation.do?type="
					+75+"&name="+t+"&email="+r+"&phone="
					+c+"&province="+a+"&city="+f+"&dealer="+l+"",function(data,textStatus,jqxhr){
							console.log("get--------------!");
							console.log(data); //undefined
							console.log(textStatus); //success
							console.log(jqxhr.status); // 200
							console.log("get--------------!");
					
							//接口返回的是:var result={"status":0}
							console.log(result.status);
							console.log("\u53c2\u6570\u6709\u8bef\uff01");
							alert("\u63d0\u4ea4\u5931\u8d25")
				});
		});
		
		
	</script>
</head>
<body>
	<form action="#" >
		<input type="" id="form_name" value="韓超"/>
	</form>
</body>
</html>

網(wǎng)站名稱:jsonp之jQuery.getScript()方法的簡(jiǎn)單使用
網(wǎng)站URL:http://muchs.cn/article38/gheisp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站網(wǎng)站收錄、網(wǎng)站排名品牌網(wǎng)站制作、企業(yè)網(wǎng)站制作、

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)