鸿蒙学习 - 数据请求 http
本地搭建json-server环境的话,可以本地测试了。 参考文章:# Macbook 上搭建 json-server www.jianshu.com/p/d222322a3…
import router from '@ohos.router'
import http from '@ohos.net.http'
@Entry
@Component
struct Index {
build() {
Column(){
Text("首页").fontSize(100)
Button("push").onClick(() => {
this.testRequestAction()
})
}
}
async testRequestAction() {
console.log("测试按钮");
// 查询
let httpRequest = http.createHttp()
let res = await httpRequest.request("http://192.168.3.14:3000/userList", {
header:{},
method: http.RequestMethod.GET,
extraData:{
}
})
console.log("请求结果是:" + `${JSON.stringify(res)}`)
// 新增数据
// let httpRequest = http.createHttp()
// httpRequest.request("http://192.168.3.14:3000/userList", {
// header:{},
// method: http.RequestMethod.POST,
// extraData:{
// "id": 5,
// "name": "哈000"
// }
// }).then(res => {
// console.log(`${res.responseCode}`)
// console.log("请求结果是:" + `${JSON.stringify(res.result)}`)
// })
}
}
第三方数据请求 axios
// 第三方数据请求 import axios from '@ohos/axios'
let res = await axios({
url: "https://m.maizuo.com/gateway?cityId=110100&pageNum=1&pageSize=10&type=1&k=2384161",
method: http.RequestMethod.GET,
headers: {
'X-Host': 'mall.film-ticket.film.list'
},
})
console.log(res.data) // 返回的数据