j***@163.com
j***@163.com
  • 发布:2020-09-09 14:19
  • 更新:2020-09-09 14:19
  • 阅读:1757

uniapp中uni.request请求封装之后,请求接口一个正常,一个出现415,大佬帮忙看一下这两个封装有什么不同

分类:uni-app

封装的请求

const baseUrl = 根路径;    //请求地址  
const httpRequest = (opts, data) => {  
    let httpDefaultOpts = {  
        url: baseUrl+opts.url,  
        data: data,  
        beforeSend :function(xmlHttp){  
            xmlHttp.setRequestHeader("If-Modified-Since","0");   
            xmlHttp.setRequestHeader("Cache-Control","no-cache");  
        },  
        method: opts.method,  
        header: opts.method == 'GET' ? {  
        'X-Requested-With': 'XMLHttpRequest',  
        "Accept": "application/json",  
        "Content-Type": "application/json; charset=UTF-8",  
    } : {  
        "Content-Type": "application/json; charset=UTF-8",  
        // 'content-type': 'application/x-www-form-urlencoded',  
    },  
        dataType: 'json',  
    }  
    let promise = new Promise(function(resolve, reject) {  
        uni.request(httpDefaultOpts).then(  
            (res) => {  
                resolve(res[1].data)  
            }  
        ).catch(  
            (response) => {  
                reject(response)  
            }  
        )  
    })  
    return promise  
};  
export default {  
    baseUrl,  
    httpRequest  
}

请求接口时候的写法

this.$http.httpRequest({  
                    url: '',  
                    method: 'POST'  
                },{  
                    参数1:参数1,  
                }).then(res =>{  

                })

这样封装的请求是成功的,get请求和post请求都可以


-这个是封装的第二个

const baseUrl = 根路径;  
const httpRequest = {  
    //请求地址  
    get(url, data) {  
        let httpDefaultOpts = {  
            url: baseUrl + url,  
            data: data,  
            beforeSend: function (xmlHttp) {  
                xmlHttp.setRequestHeader("If-Modified-Since", "0");  
                xmlHttp.setRequestHeader("Cache-Control", "no-cache");  
            },  
            method: 'GET',  
            header: {  
                'X-Requested-With': 'XMLHttpRequest',  
                "Accept": "application/json",  
                "Content-Type": "application/json; charset=UTF-8",  
            },  
            dataType: 'json',  
        }  
        return this.getPromise(httpDefaultOpts);  
    },  
    post(url, data) {  
        let httpDefaultOpts = {  
            url: baseUrl + url,  
            data: data,  
            beforeSend: function (xmlHttp) {  
                xmlHttp.setRequestHeader("If-Modified-Since", "0");  
                xmlHttp.setRequestHeader("Cache-Control", "no-cache");  
            },  
            method: 'POST',  
            header: {  
                "Content-Type": "application/json; charset=UTF-8",  
                //  'Content-Type': 'application/json',  
            },  
            dataType: 'json',  
        }  
        return this.getPromise(httpDefaultOpts);  
    },  
    getPromise(httpDefaultOpts){  
        let promise = new Promise(function (resolve, reject) {  
            uni.request(httpDefaultOpts).then(  
                (res) => {  

                    resolve(res[1].data)  
                }  
            ).catch(  
                (response) => {  
                    reject(response)  
                }  
            )  
        })  
        return promise  
    }  
}  

export default {  
    baseUrl,  
    httpRequest  
}  

这是第二个请求接口时候的写法

this.$http.httpRequest.post('',  
                    {  
                        参数1:参数1,  
                    }).then(res =>{  

                })

第二个请求接口的时候get请求是可以的,但是post请求会出现415状态码,我看这两个没什么区别啊,自己菜鸡一枚,还在学习中,希望大佬能指点一下封装的这两个有什么区别吗?为什么第二个post请求会失败呢?

2020-09-09 14:19 负责人:无 分享
已邀请:

该问题目前已经被锁定, 无法添加新回复