uni-app:配置访问接口时的跨域(hbuilderx: 3.6.18)
2023-09-14 08:59:32 时间
一,未配置跨域时的报错信息:
Access to XMLHttpRequest at 'http://api.lhdtest.net/item/list' from origin 'http://localhost:8081' has been blocked by CORS policy:
No 'Access-Control-Allow-Origin' header is present on the requested resource. 15:00:59.629 chunk-vendors.js:14781 GET http://api.lhdtest.net/item/list net::ERR_FAILED 200 (OK) l @ chunk-vendors.js:14781 (anonymous) @ chunk-vendors.js:14781 O @ chunk-vendors.js:14781 (anonymous) @ chunk-vendors.js:14781 (anonymous) @ chunk-vendors.js:14781 getItem @ item.vue:19 click @ pages-item-item.js:54 invokeWithErrorHandling @ chunk-vendors.js:4196 invoker @ chunk-vendors.js:4521 invokeWithErrorHandling @ chunk-vendors.js:4196 invoker @ chunk-vendors.js:4517 original._wrapper @ chunk-vendors.js:9405 15:00:59.629 item.vue:26 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'statusCode') at eval (item.vue:26:1)
说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
或: https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,代码:
<template> <view> <button style="width:700rpx;" type="primary" @click="getItem">访问接口</button> </view> </template> <script> export default { data() { return { } }, methods: { getItem(){ uni.showToast({ title:"已点击了按钮" }); uni.request({ url:'/api/item/list', method:'get', }).then((result)=>{ let [error,res] = result; //result将返回一个数组[error,{NativeData}] if(res.statusCode === 200){ console.log(res.data); } if(res.statusCode === 404){ console.log('请求的接口没有找到'); } }) }, } } </script> <style> </style>
三,配置跨域
编辑manifest.json->选择"源码视图"->增加h5一栏的配置如下:
![](https://img2023.cnblogs.com/blog/1938691/202302/1938691-20230212152802158-542753744.png)
内容:
"h5":{ "devServer" : { "https" : false, "disableHostCheck":true, "proxy" : { "/api" : { "target" : "http://api.lhdtest.net", //域名 "changeOrigin" : true, "secure" : false, "pathRewrite" : { "^/api" : "" } } } } },
四,测试效果:
![](https://img2023.cnblogs.com/blog/1938691/202302/1938691-20230212153228448-1912518514.png)
已可返回接口的数据,如图:
五,查看hbuilderx的版本:
相关文章
- Hybrid App技术批量制作APP应用与跨平台解决方案
- 移动web app开发必备 - Deferred 源码分析
- 在Web.config或App.config中的添加自定义配置
- uni-app:配置底部tabbar的例子(hbuilderx 3.6.18)
- Create react App eject后 webpack 的优化配置
- 创建自定义配置节点(web.config和app.config都适用)
- uni-app:配置底部tabbar的例子(hbuilderx 3.6.18)
- uni-app:配置访问接口时的跨域(hbuilderx: 3.6.18)
- Atitit 调用百度语音识别 目录 1. 建立一个音频app项目,获得appid kersec1 2. 直接使用JAR包步骤如下:1 2.1. public class baiduAudio
- ios xcode 怎样模拟app被挂起(在后台被系统杀死) APP 的五种状态
- ios xcode app被杀死了怎样打印调试 调试手动点开的APP 被系统唤醒的app的调试
- iOS app store无法提交以供审核 App隐私
- iOS 性能优化(一):Improving Your App‘s Performance
- App Thinning(为什么苹果app上传时的包比在appStore下载下来的包大很多)
- android 11.0 12.0SystemUI屏蔽某个app的通知
- Android 进入其他app和退出App杀死进程的方法详解
- 〖Python APP 自动化测试实战篇⑨〗- 实战 - App自动化实战操作
- Appium如何简化混合App的测试和自动化
- Flask app.config 的配置
- 微信小程序_uni-app_轮播图组件_swiper