bianyuan456
bianyuan456
  • 发布:2019-12-05 22:32
  • 更新:2019-12-06 14:02
  • 阅读:1107

新手问题,怎么在主函数里面调用回调函数的值呢?跨域带cookie的http访问遇到了这个问题

分类:uni-app

详细问题描述

跨域带cookie的http访问,目前web端实现的技术方案就html5+引擎的plus.net.XMLHttpRequest能做到吧,项目需要这个,不会写那种放到common/*.js然后在vue页面中import引用的,就在每个页面写了个myhttp函数,但是这样也是遇到了问题,怎么在主函数里面调用回调函数的值呢?另外我还想把myhttp变成同步的方法,就是很多http访问,等前面的http访问进行完了,再进行后面的http访问的。
新人问题好多,麻烦给个提示把,我知道promise和callback,但是自己写,写不出来,看勉强能看懂。。
另外除了h5+引擎能让ios和Android的app实现跨域带cookie访问,普通浏览器能访问的web网页上有没有方法实现跨域带cookie访问?
[内容]
以下内容用hbuilderx 新建一个uniapp默认项目。在index.vue直接复制上就行.项目也放到附件里了。

<template>  
    <view class="content">  
        <button @click="test">测试</button>  
        <text>{{ret}}</text>  
    </view>  
</template>  

<script>  
    var _self;  
    export default {  
        data() {  
            return {  
                ret: ''  
            }  
        },  
        onLoad() {  
            _self = this;  
        },  

        methods: {  
            test() {  
                let str = _self.myhttp("https://www.dcloud.io/docs/api/zh_cn/xhr.html", "0",  
                    "", 'uin=o0555555; skey=@666;');  
                console.log(2,str);//这里显示不出1的内容,想让它显示1的内容,也就是myhttp的返回内容  
            },  
            myhttp(url, method, data, cookies, header, wait) {  
                let xhr = new plus.net.XMLHttpRequest();  
                let method_ = ((method == "1") ? "POST" : "GET");  
                xhr.onloadend = function() {  
                    if (xhr.status == 200) {  
                        let str = xhr.responseText;  
                        console.log(1,str);//这里能输出想要的内容,这个str就是想返回的变量!  
                        _self.ret = str;  
                    }   
                }  
                xhr.open(method_, url);  
                xhr.setRequestHeader('Cookie', cookies);  
                if (method_ == "POST") {  
                    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');  
                }  
                xhr.timeout = 5000;  
                xhr.send(data);  
            },  

        }  
    }  
</script>  

<style>  
</style>  

[步骤]

2019-12-05 22:32 负责人:无 分享
已邀请:
琦琦宝贝

琦琦宝贝 - - 全职接单 高效率、高质量 uniapp项目开发 服务器部署-运维 欢迎咨询 Q:406242283 vx:shouji_18131126375

在myhttp回调里面执行 this.test()

  • bianyuan456 (作者)

    谢谢,不过还是不懂怎么改。

    2019-12-06 11:39

哆来梦

哆来梦 - 313712831@qq.com

创建新文件 例如名称为app_request.js。内容如下:  
const app = {  
    async request(url, method, data, cookies, header, wait) {  
        return new Promise((resolve, reject) => {  
            let xhr = new plus.net.XMLHttpRequest();  
            let method_ = ((method == "1") ? "POST" : "GET");  
            xhr.open(method_, url);  
            xhr.setRequestHeader('Cookie', cookies);  
            if (method_ == "POST") {  
                xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');  
            }  
            xhr.timeout = 5000;  
            xhr.send(data);  
            xhr.onloadend = function() {  
                console.log(xhr); //这里能输出想要的内容,这个str就是想返回的变量!  
                if (xhr.status == 200) {  
                    let str = xhr.responseText;  
                    // console.log(1, str); //这里能输出想要的内容,这个str就是想返回的变量!    
                    resolve(str);  
                    // resolve('请求成功');  
                } else {  
                    resolve(false);  
                }  
            };  
            xhr.onerror = () => {  
                resolve(false)  
            };  
        })  
    }  
}  
export default app;  

页面引入:  

    import app_request from '../../utils/app_request.js';  // 相对路径  

使用:  

            async test() {  
                    const str = await app_request.request("https://www.dcloud.io/docs/api/zh_cn/xhr.html", "0", "",  
                        'uin=o0555555; skey=@666;');  
                    console.log('str' + JSON.stringify(str))  
            },
  • bianyuan456 (作者)

    谢谢!已经发15软妹币红包表示感谢!你的回答解决了我的问题

    2019-12-06 15:16

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