vue-ajax小封裝實例
更新時間:2017年09月18日 08:50:53 作者:涼宮
下面小編就為大家?guī)硪黄獀ue-ajax小封裝實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
1. js 文件:
/*
* 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格式 (可省略) (設置為 "json")
*
* 3. new Vue().ajax.get().cancel(): 取消異步請求
* 4. new Vue().ajax.json(str): 可轉(zhuǎn)化json格式字符串
**/
Vue.prototype.ajax={
//添加url傳送信息
addUrl: function (url,obj){
//如果省略url,則為post請求,令obj為url,令url為null
if(arguments.length==1){
obj=url;
url=null;
}
//url不為空(get請求: 設置url信息)
if(!!url){
for(var k in obj){
url += (url.indexOf("?")==-1 ? "?" : "&");
url+=encodeURIComponent(k)+ "=" +encodeURIComponent(obj[k]);
}
}else{
//post請求(設置data信息)
url="";
for(var k in obj){
url+=encodeURIComponent(k)+ "=" +encodeURIComponent(obj[k]);
url+="&";
}
//刪除最后一個&
var arr=url.split("");
arr.pop();
url=arr.join("");
}
//返回拼接好的信息
return url;
},
get: function (url,data,fn,ojson){
this.xhr=new XMLHttpRequest();
//省略data時,即不發(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);
//當請求完成之后調(diào)用回調(diào)函數(shù)返回數(shù)據(jù)
this.success(fn,ojson);
//鏈式編程
return this;
},
post: function (url,data,fn,ojson){
this.xhr=new XMLHttpRequest();
//省略data時,即不發(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);
//當請求完成之后調(diào)用回調(diào)函數(shù)返回數(shù)據(jù)
this.success(fn,ojson);
//鏈式編程
return this;
},
//字符串轉(zhuǎn)換json
json: function (str){
return (new Function("return " + str))();
},
success: function (fn,ojson){
//當請求完成之后調(diào)用回調(diào)函數(shù)返回數(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);
}
}
}
},
//取消異步請求
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">點擊獲取信息</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. 結果


以上這篇vue-ajax小封裝實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue框架和react框架的區(qū)別以及各自的應用場景使用
這篇文章主要介紹了vue框架和react框架的區(qū)別以及各自的應用場景使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
Vue ElementUi同時校驗多個表單(巧用new promise)
這篇文章主要介紹了巧用new promise實現(xiàn)Vue ElementUi同時校驗多個表單功能,實現(xiàn)的方法有很多種,本文給大家?guī)淼氖且环N比較完美的方案,需要的朋友可以參考下2018-06-06
vue中的<template>標簽與react中的<></>標簽區(qū)別詳解
這篇文章主要為大家介紹了vue中的<template>標簽與react中的<></>標簽區(qū)別詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08
vue中input type=file上傳后@change事件無效的解決方案
這篇文章主要介紹了vue中input type=file上傳后@change事件無效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05

