1. js 文件:
創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、修文網(wǎng)絡(luò)推廣、重慶小程序開發(fā)、修文網(wǎng)絡(luò)營銷、修文企業(yè)策劃、修文品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供修文建站搭建服務(wù),24小時(shí)服務(wù)熱線:028-86922220,官方網(wǎng)址:muchs.cn
/* * ajax封裝: * 1. 引入文件 * 2. new Vue().ajax.get(url,data,fn,ojson), 或 new Vue().ajax.post(url,data,fn,ojson) * url: 需要獲取數(shù)據(jù)的文件地址 (string) * data: 需要發(fā)送的信息 (可省略) (obj) * fn: 獲取信息后的回調(diào)函數(shù),接收到的返回值為data (function) * ojson: 是否需要轉(zhuǎn)換為json格式 (可省略) (設(shè)置為 "json") * * 3. new Vue().ajax.get().cancel(): 取消異步請(qǐng)求 * 4. new Vue().ajax.json(str): 可轉(zhuǎn)化json格式字符串 **/ Vue.prototype.ajax={ //添加url傳送信息 addUrl: function (url,obj){ //如果省略u(píng)rl,則為post請(qǐng)求,令obj為url,令url為null if(arguments.length==1){ obj=url; url=null; } //url不為空(get請(qǐng)求: 設(shè)置url信息) if(!!url){ for(var k in obj){ url += (url.indexOf("?")==-1 ? "?" : "&"); url+=encodeURIComponent(k)+ "=" +encodeURIComponent(obj[k]); } }else{ //post請(qǐng)求(設(shè)置data信息) url=""; for(var k in obj){ url+=encodeURIComponent(k)+ "=" +encodeURIComponent(obj[k]); url+="&"; } //刪除最后一個(gè)& var arr=url.split(""); arr.pop(); url=arr.join(""); } //返回拼接好的信息 return url; }, get: function (url,data,fn,ojson){ this.xhr=new XMLHttpRequest(); //省略data時(shí),即不發(fā)送數(shù)據(jù) if(typeof data =="function"){ ojson=fn; fn=data; data={}; } //合并url和data信息 url=this.addUrl(url,data) //是否異步發(fā)送 this.xhr.open("get",url,true); this.xhr.send(null); //當(dāng)請(qǐng)求完成之后調(diào)用回調(diào)函數(shù)返回?cái)?shù)據(jù) this.success(fn,ojson); //鏈?zhǔn)骄幊? return this; }, post: function (url,data,fn,ojson){ this.xhr=new XMLHttpRequest(); //省略data時(shí),即不發(fā)送數(shù)據(jù) if(typeof data =="function"){ ojson=fn; fn=data; data={}; } //合并data信息 data=this.addUrl(data); //是否異步發(fā)送 this.xhr.open("post",url,true); this.xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); this.xhr.send(data); //當(dāng)請(qǐng)求完成之后調(diào)用回調(diào)函數(shù)返回?cái)?shù)據(jù) this.success(fn,ojson); //鏈?zhǔn)骄幊? return this; }, //字符串轉(zhuǎn)換json json: function (str){ return (new Function("return " + str))(); }, success: function (fn,ojson){ //當(dāng)請(qǐng)求完成之后調(diào)用回調(diào)函數(shù)返回?cái)?shù)據(jù) var self=this; this.xhr.onreadystatechange=function (){ var odata; if(self.xhr.readyState == 4){ if((self.xhr.status>=200 && self.xhr.status<300) || self.xhr.status == 304){ odata=self.xhr.responseText; //若為json則轉(zhuǎn)化json格式 if(ojson==="json"){ odata=self.json(odata); } fn(odata); }else{ odata="request was unsuccessful: "+self.xhr.status; fn(odata); } } } }, //取消異步請(qǐng)求 cancel: function (){ this.xhr.abort(); return this; } }
2. html示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <div id="app"> <button @click="getInfo">點(diǎn)擊獲取信息</button> <span>{{ msg }}</span> </div> <script src="vue.js"></script> <script src="vue-ajax.js"></script> <script> var vm=new Vue({ el: "#app", data: { msg: "", }, methods: { getInfo: function (){ var self=this; this.ajax.get("1.json",{ tel: 123456, address: "池州市" },function (data){ self.msg=data[1].name },"json"); } } }) </script> </body> </html>
3. 需要獲取的數(shù)據(jù)(1.json)
[ { "name": "張三", "age": 18, "sex": "man" }, { "name": "李四", "age": 20, "sex": "woman" }, { "name": "王五", "age": 22, "sex": "man" } ]
4. 結(jié)果
以上這篇vue-ajax小封裝實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持創(chuàng)新互聯(lián)。
當(dāng)前文章:vue-ajax小封裝實(shí)例
當(dāng)前鏈接:http://muchs.cn/article48/ighchp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、網(wǎng)站導(dǎo)航、網(wǎng)站營銷、網(wǎng)站收錄、外貿(mào)網(wǎng)站建設(shè)、外貿(mào)建站
聲明:本網(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í)需注明來源: 創(chuàng)新互聯(lián)