一、适用于浏览器跨域
1.manifest.json文件新增
"h5": {
"devServer": {
"port": 8000,
"disableHostCheck": true,
"proxy" : {
"/api" : {
"target" : "https://www.xxxxxx.com", // 需要跨域的域名
"changeOrigin" : true,
"secure" : false,
"pathRewrite" : {
"^/api" : ""
}
}
}
}
}
2.下一步页面内直接调接口;
uni.request({
url: "api/netease/songList",
dataType: "JSON",
success: function(res) {
console.log(res)
}
});
二、上面的方法在app中请求不到数据
找到 main.js文件
Vue.prototype.$Url = 'https://api.douban.com';
2.页面内调用接口
uni.request({
url: t.$Url + '/xxxxxx',
dataType: 'json', //默认 json格式
method: 'POST', //请求方式
success: (res) => {
console.log(res)
},
fail: (error) => {
console.log(error)
}
})