更新时间:2022-01-07 15:33:28浏览次数:611+次
这是在鸿蒙应用开发中的一例,其数据请求拿不到返回值,以下是原因及处理对策。
问题的产生
以下是看文档写的请求代码:
import http from '@ohos.net.http';
export default {
data: {
title: ""
},
onInit() {
this.title = this.$t('strings.world');
this.fetch()
},
fetch() {
let httpRequest = http.createHttp();
httpRequest.on('headerReceive', (err, data) => {
});
httpRequest.request(
"https://snackbox.uxiao.vip/box/v1/common/box_notice/info",
{
method: 'GET',
header: {
'Content-Type': 'application/json'
},
}, (err, data) => {
console.log(JSON.stringify(data));
}
);
}
}
以下为结果:
打印出来的返回值是{"result":"[PC Preview] unknow result","responseCode":"[PC Preview] unknow responseCode","header":"[PC Preview] unknow header"}。
原因及对策
以下是网友们给出的原因及对策:
1、pc preview预览器不支持这个功能的预览,要到模拟器测试。
2、Previewer是预览器,没有网络的,Previewer预览器是用来方便开发界面,时时查看效果的,请求网络数据,可以使用真机、本地模拟器、远程模拟器、远程真机来获取到数据。
3、用真机,模拟器上看不到结果。
说明
远程模拟器、远程真机打开方法是:点击菜单Tool -> Device Manager弹出窗口,其中Remote Emulator为远程模拟器、Remote Device为远程真机。
要登录华为开发者账号后,才可以使用,使用时间为1个小时,一个小时后自动关闭,如还要使用,请重新启动模拟器。
相关资讯