在 src 目录下新建 vue.extend.js ,内容如下:

export default
{
install(Vue)
{
Vue.prototype.$http=function(options){
/*将数据转化为字符串*/
var strData=function(data){
var dataStr="";
for(var key in data){
dataStr += key+'='+data[key]+'&';
}
dataStr = dataStr && dataStr.slice(,-);
return dataStr;
};
/*创建 XMLHttpRequest 对象*/
var createXHR=function(){
var xhr;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xhr=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xhr=new ActiveXObject("Microsoft.XMLHTTP");
}
return xhr
};
/*向服务器发送请求*/
var open=function(xhr,type,url,async){
xhr.open(type,url,async);
};
var send=function(xhr,msg){
xhr.send(msg);
};
var setHeaders=function(xhr,headers){
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
if(!headers){
return false;
}
for(var key in headers){
xhr.setRequestHeader(key,headers[key]);
}
}
var request=function(xhr,opts){
if(opts.type==="GET"){
open(xhr,opts.type,opts.url+'?'+strData(opts.data),opts.async);
send(xhr,null);
}
else if(opts.type==="POST"){
open(xhr,opts.type,opts.url,opts.async);
if(opts.headers){
setHeaders(xhr,opts.headers);
}
send(xhr,strData(opts.data));
}
};
return new Promise((resolve,reject)=>{
if(!options || typeof options != 'object'){
reject(new Error("参数错误,请传入对象参数!"));
return false;
}
if(!options.url){
reject(new Error("url不能为空"));
return false;
}
options.type = options.type?options.type.toUpperCase():'GET';
options.async = (options.async && options.async === false)?false:true;
options.dataType = options.dataType || "json";
options.data = options.data || {};
options.headers = options.headers || {};
var dataStr=strData(options.data);
/*创建 XMLHttpRequest 对象*/
var xhr=createXHR();
/*创建服务器返回响应后执行操作函数*/
xhr.onreadystatechange=function(){
var responseData;
if(xhr.readyState == ){
switch (xhr.status){
case :
switch (options.dataType){
case "xml":
responseData=xhr.responseXML;
break;
case "text":
responseData = xhr.responseText;
break;
case "json":
responseData = JSON.parse(xhr.responseText);
break;
}
resolve(responseData);
default:
reject(new Error("这里做错误处理"));
}
}
};
/*向服务器发送请求*/
request(xhr,options);
})
};
Vue.prototype.$post=function(options){
options.type='post';
return this.$http(options);
};
Vue.prototype.$get=function(options){
options.type='get';
return this.$http(options);
};
}
}

在 main.js 中引入vue.extend.js

import utils from './vue.extend'
Vue.use(utils);

然后就可以通过this.$http、this.$get、this.$post使用啦

      this.$http({
url:"https://api.api68.com/klsf/getLotteryInfo.do?issue=&lotCode=10009",
type:"get"
})
.then(function(res){
console.log("$http",res);
console.log(this.msg);
}.bind(this))
.catch(function(err){
console.log(err)
}.bind(this))
      this.$get({
url:"https://api.api68.com/klsf/getLotteryInfo.do?issue=&lotCode=10009"
})
.then(function(res){
console.log("$get",res);
console.log(this.msg);
}.bind(this))
.catch(function(err){
console.log(err)
}.bind(this))
this.$post({
url:"https://api.api68.com/klsf/getLotteryInfo.do",
data:{
issue:"",
lotCode:""
}
})
.then(function(res){
console.log("$post",res);
console.log(this.msg);
}.bind(this))
.catch(function(err){
console.log(err)
}.bind(this))

在vue项目中使用自己封装的ajax的更多相关文章

  1. vue项目中的函数封装

    项目中一般都会有fun.js这类的文件,里面有各种的如转换时间格式的,处理转换的等等函数方法. 其实经常用到的去获取基本数据的接口也可以封装成一个方法,方便复用. 如上面所标,获取列表数据之前需要先获 ...

  2. vue项目中axios的封装和使用

    一.axios的功能特点 在浏览器中发送 XMLHttpRequests 请求 在 node.js 中发送 http请求 支持 Promise API 拦截请求和响应 转换请求和响应数据 支持多种请求 ...

  3. 浅谈 Axios 在 Vue 项目中的使用

    介绍 Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中. 特性 它主要有如下特性: 浏览器端发起XMLHttpRequests请求 Node端发起http ...

  4. vue项目中遇到的那些事。

    前言 有好几天没更新文章了.这段实际忙着做了一个vue的项目,从 19 天前开始,到今天刚好 20 天,独立完成. 做vue项目做这个项目一方面能为工作做一些准备,一方面也精进一下技术. 技术栈:vu ...

  5. vue项目中 axios 和Vue-axios的关系

    文章收集于:https://segmentfault.com/q/1010000010812113 在vue项目中,会经常看到如下代码:   今天看到有些项目是这样写的,就有点看不懂了.  ----解 ...

  6. Vue项目中使用Vuex + axios发送请求

    本文是受多篇类似博文的影响写成的,内容也大致相同.无意抄袭,只是为了总结出一份自己的经验. 一直以来,在使用Vue进行开发时,每当涉及到前后端交互都是在每个函数中单独的写代码,这样一来加大了工作量,二 ...

  7. 在vue项目中的axios使用配置记录

    默认vue项目中已经安装axios,基于element-ui开发,主要记录配置的相关. axiosConfig.js import Vue from 'vue' import axios from ' ...

  8. 如何在Vue项目中给路由跳转加上进度条

    1.前言 在平常浏览网页时,我们会注意到在有的网站中,当点击页面中的链接进行路由跳转时,页面顶部会有一个进度条,用来标示页面跳转的进度(如下图所示).虽然实际用处不大,但是对用户来说,有个进度条会大大 ...

  9. Vue项目中的http请求统一管理

    module.exports = { dev: { // Paths assetsSubDirectory: '/', assetsPublicPath: '/', proxyTable: { /op ...

随机推荐

  1. 如何录制高清GIF格式的图片

    如何录制高清GIF格式的图片 工具:傲软GIF 下载地址:https://www.apowersoft.cn/gif 特点:质量高,能够一帧一帧的修改 使用简单.就不说了.自行尝试.这里只是提供一个制 ...

  2. 终端下更改printk打印级别

    如何去更改printk的打印级别? 1.查看当前控制台的打印级别 # cat /proc/sys/kernel/printk 该文件有4个数字值,它们根据日志记录消息的重要性,定义将其发送到何处,上面 ...

  3. python定时任务APScheduler

    APScheduler定时任务 APScheduler 支持三种调度任务:固定时间间隔,固定时间点(日期),Linux 下的 Crontab 命令.同时,它还支持异步执行.后台执行调度任务. 一.基本 ...

  4. cisco ap客户端无规律掉线

    设备 cisco air-ct2504-50-k9 cisco air-ap1832I-H-k9 首先根据这个帖子 https://community.cisco.com/t5/other-wirel ...

  5. Python代码约定

    建议遵守以下约定: 使用 4 个空格来缩进 永远不要混用空格和制表符 在函数之间空一行 在类之间空两行 字典,列表,元组以及参数列表中,在 , 后添加一个空格.对于字典,: 后面也添加一个空格 在赋值 ...

  6. 在Grafana使用普罗米修斯

    aaarticlea/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPCEtLSBHZW5lcmF0b3I6IE ...

  7. Codeforces Round #604 (Div. 2) (题解)

    A. Beautiful String (暴力) 题目链接 题目大意: 给定一个字符串,只有 \(?a\ b\ c\ ?\) ,问是否存在一种将所有的 \(?\) 替换成 \(a\ b\ c\) ,使 ...

  8. Django-07-Model操作

    一.数据库的配置 1. 数据库支持 django默认支持sqlite.mysql.oracle.postgresql数据库  <1> sqlite django默认使用sqlite的数据库 ...

  9. 第二周、ubuntu的简单介绍与使用

    一.复习第一周的大致内容:1.UNIX与Linux.ubuntu系统UNIX是1971年贝尔实验室的肯·汤普逊.丹尼斯·里奇,合作研发一款通过的操作系统,多用户.多任务.安全.稳定,收费.Linux是 ...

  10. 挂载一个NFS共享

    在 system2 上挂载一个来自 system1.group8.example.com 的NFS共享,并符合下列要求: 1./public 挂载在下面的目录上 /mnt/nfsmount 2./pr ...