网络请求数据后的处理

做了一个网络请求,请求到的数据无法获取,如图:

网络请求数据后的处理-鸿蒙开发者社区

我要拿到里面的msg和code参数,如何获取呢?

HarmonyOS
2024-09-11 09:44:35
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

可以参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-http-V5

参考Demo:

// xxx.ets 
import http from '@ohos.net.http'; 
 
@Entry 
@Component 
struct WebComponent { 
  private account:string="123344"; 
  private password:string="123344"; 
  build() { 
    Column() { 
      Text("测试网络请求11111").onClick(()=>{ 
        let httpRequest = http.createHttp(); 
        let requestUrl = "https://example/uniapp/"; 
        httpRequest.request(requestUrl,{ 
          method: http.RequestMethod.POST, 
          header: { 
            'Content-Type': 'application/json' 
          }, 
          extraData: { 
            action: "login", 
            username: this.account, 
            password: this.password, 
          }, 
        }, (err, data) => { 
          if (!err) { 
            console.log(JSON.stringify(data.result)); 
            //第一种方法打印code 
            let code:number=JSON.parse(data.result+"").code; 
            console.log(code+""); 
            interface dataResult { 
              code : number 
              msg : string 
              data: string[] ; 
            } 
            //第二种方法打印出msg 
            let dataObj:dataResult = JSON.parse(data.result as string); 
            console.info(dataObj.msg); 
            httpRequest.destroy(); 
          } else { 
            console.info('error:' + JSON.stringify(err)); 
            httpRequest.off('headersReceive'); 
            httpRequest.destroy(); 
          } 
        } 
        ); 
      }).backgroundColor(Color.Red).width(100).height(200) 
    } 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
分享
微博
QQ
微信
回复
2024-09-11 17:13:39


相关问题
网络请求数据处理问题
876浏览 • 1回复 待解决
HarmonyOS 接口请求数据处理
407浏览 • 1回复 待解决
网络请求使用gzip压缩数据
2646浏览 • 1回复 待解决
HarmonyOS 网络请求结果数据解析
424浏览 • 1回复 待解决
网络请求创建,持久化问题
1268浏览 • 1回复 待解决
HarmonyOS网络请求JSON转换问题
1541浏览 • 1回复 待解决
网络请求如何进行回调?
1597浏览 • 1回复 待解决
HarmonyOS rcp网络请求返回数据为null
635浏览 • 1回复 待解决