当前位置:网站首页>【踩坑系列】uniapp之h5 跨域的问题
【踩坑系列】uniapp之h5 跨域的问题
2022-07-07 04:56:00 【涛涛之海】
【踩坑系列】uniapp之h5 跨域的问题
前言
在浏览器上调试移动端的界面,出现 不允许出现多个 ‘Access-Control-Allow-Origin’ CORS 头信息。 而且看到Response 返回的信息,以为是服务器上出现配置了多次 跨域访问的设置,才导致的问题,所以一直在找后端上的问题,花了一段时间排查后,并没有配置多次跨域访问的设置,而且移动端的真机上可以正常访问,这才扭转了排查的思路,从前端上面入手解决问题。

什么是跨域
跨域是浏览器的专用概念,指js代码访问自己来源站点之外的站点。比如A站点网页中的js代码,请求了B站点的数据,就是跨域。
A和B要想被认为是同域,则必须有相同的协议(比如http和https就不行)、相同域名、和相同端口号(port)。
如果你是做App、小程序等非H5平台,是不涉及跨域问题的。

如何解决
解决方案:uni-app官方推荐使用HBuilderX中内置的浏览器去预览,在内置的浏览器中不会存在跨域问题;但是要是在Chrome 浏览器中预览的话就会出现这个跨域问题,官方推荐使用安装Allow-Control-Allow-Origin: * 插件的方式去解决。参考:uni-app H5跨域问题解决方案(CORS、Cross-Origin)。
# 是否允许跨域访问 CORS,如果允许,设置允许的域名
accessControlAllowOrigin: '*'
# 允许跨域访问时 CORS,可以获取和返回的方法和请求头
accessControlAllowMethods: GET, POST, OPTIONS
accessControlAllowHeaders: content-type, x-requested-with, x-ajax, x-token, x-remember
accessControlExposeHeaders: x-remember
参考文章
边栏推荐
- Leetcode 90: subset II
- Wechat applet data binding multiple data
- C language queue
- Linux server development, MySQL stored procedures, functions and triggers
- C language communication travel card background system
- 这5个摸鱼神器太火了!程序员:知道了快删!
- Recursive method constructs binary tree from middle order and post order traversal sequence
- These five fishing artifacts are too hot! Programmer: I know, delete it quickly!
- Chip design data download
- Operation suggestions for today's spot Silver
猜你喜欢

青龙面板-今日头条

Explore Cassandra's decentralized distributed architecture

运放电路的反馈电阻上并联一个电容是什么作用

Leetcode 90: subset II

Qt学习27 应用程序中的主窗口

Es FAQ summary

Linux server development, MySQL index principle and optimization

Wechat applet data binding multiple data

2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案

【数字IC验证快速入门】15、SystemVerilog学习之基本语法2(操作符、类型转换、循环、Task/Function...内含实践练习)
随机推荐
Why should we understand the trend of spot gold?
2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案
Ansible
Pytest+allure+jenkins installation problem: pytest: error: unrecognized arguments: --alluredir
芯片资料 网站 易特创芯
2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions
Few shot Learning & meta learning: small sample learning principle and Siamese network structure (I)
【数字IC验证快速入门】12、SystemVerilog TestBench(SVTB)入门
Recursive method to construct binary tree from preorder and inorder traversal sequence
运放电路的反馈电阻上并联一个电容是什么作用
3D reconstruction - stereo correction
Paddlepaddle 29 dynamically modify the network structure without model definition code (relu changes to prelu, conv2d changes to conv3d, 2D semantic segmentation model changes to 3D semantic segmentat
[UVM foundation] what is transaction
Regular e-commerce problems part1
[UVM basics] summary of important knowledge points of "UVM practice" (continuous update...)
Main window in QT learning 27 application
Custom class loader loads network class
Li Kou interview question 04.01 Path between nodes
这5个摸鱼神器太火了!程序员:知道了快删!
[quickstart to Digital IC Validation] 15. Basic syntax for SystemVerilog Learning 2 (operator, type conversion, loop, Task / Function... Including practical exercises)