鱼C论坛

 找回密码
 立即注册
查看: 1349|回复: 4

[已解决]求助,问题求解决

[复制链接]
发表于 2022-8-5 21:45:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
js不能对axios请求的html使用document系列的函数?
最佳答案
2022-8-5 21:57:32
1、新建 httpRequest.js 文件,定义如下方法:

//axios封装post请求
function axiosPostRequst(url,data) {
    let result = axios({
        method: 'post',
        url: url,
        data: data,
        transformRequest:[function(data){
            let ret = '';
            for(let i in data){
                ret += encodeURIComponent(i)+'='+encodeURIComponent(data[i])+"&";
            }
            return ret;
        }],
        header:{
            'Content-Type':'application/x-www-form-urlencoded'
        }
    }).then(resp=> {
        return resp.data;
    }).catch(error=>{
        return "exception="+error;
    });
    return result;
}

//get请求
function axiosGetRequst(url) {
    var result = axios({
        method: 'get',
        url: url
    }).then(function (resp) {
        return resp.data;
    }).catch(function (error) {
        return "exception=" + error;
    });
    return result;
}

2、在html中引入vue.js、axios.js、httpRequest.js

<!--根据自己路径引入-->
<script src="js/vue.min.js"></script>
<script src="js/axios-0.19.js"></script>
<script src="js/httpRequest.js"></script>
3、在js文件中使用封装好的方法

var datas = {
    flag:'pass',
    comment:'通过'
};
axiosPostRequst('http://localhost:10001/account/approve',datas).then(result=>{
    consloe.log(result);
});
ok,大功告成,这样就不用每次请求时写一大串请求头、请求体了。
网上回答的答案,我之前也想过这个问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-8-5 21:57:32 | 显示全部楼层    本楼为最佳答案   
1、新建 httpRequest.js 文件,定义如下方法:

//axios封装post请求
function axiosPostRequst(url,data) {
    let result = axios({
        method: 'post',
        url: url,
        data: data,
        transformRequest:[function(data){
            let ret = '';
            for(let i in data){
                ret += encodeURIComponent(i)+'='+encodeURIComponent(data[i])+"&";
            }
            return ret;
        }],
        header:{
            'Content-Type':'application/x-www-form-urlencoded'
        }
    }).then(resp=> {
        return resp.data;
    }).catch(error=>{
        return "exception="+error;
    });
    return result;
}

//get请求
function axiosGetRequst(url) {
    var result = axios({
        method: 'get',
        url: url
    }).then(function (resp) {
        return resp.data;
    }).catch(function (error) {
        return "exception=" + error;
    });
    return result;
}

2、在html中引入vue.js、axios.js、httpRequest.js

<!--根据自己路径引入-->
<script src="js/vue.min.js"></script>
<script src="js/axios-0.19.js"></script>
<script src="js/httpRequest.js"></script>
3、在js文件中使用封装好的方法

var datas = {
    flag:'pass',
    comment:'通过'
};
axiosPostRequst('http://localhost:10001/account/approve',datas).then(result=>{
    consloe.log(result);
});
ok,大功告成,这样就不用每次请求时写一大串请求头、请求体了。
网上回答的答案,我之前也想过这个问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-8-5 22:54:24 | 显示全部楼层
滴滴滴
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-8-6 06:40:56 From FishC Mobile | 显示全部楼层
代码???
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-8-9 13:47:16 | 显示全部楼层
?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 11:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表