首页 > 其他分享 >52.同源策略(Same-Origin Policy)限制了跨域请求No 'Access-Control-Allow-Origin' header is present on th

52.同源策略(Same-Origin Policy)限制了跨域请求No 'Access-Control-Allow-Origin' header is present on th

时间:2023-05-29 14:12:25浏览次数:63  
标签:Origin Control 跨域 requested res Access Allow

又遇到如下报错了,该如何处理,

Access to XMLHttpRequest at 'http://localhost:3000/users' from origin 'http://localhost:5173' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

这个错误是因为浏览器的同源策略(Same-Origin Policy)限制了跨域请求。在默认情况下,浏览器只允许在同一域名下的请求。如果你的前端代码和后端代码不在同一个域名下,就会出现这个错误。

解决这个问题的方法有两种:

  1. 在后端添加CORS头部信息

在后端代码中添加如下代码,即可解决跨域问题:

app.use((req, res, next) => {
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
  res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
  res.setHeader('Access-Control-Allow-Credentials', true);
  next();
});

这段代码会在每个请求的响应头中添加CORS头部信息,允许所有域名的请求访问。

  1. 使用代理

前端代码中可以使用代理来解决跨域问题。在Vue CLI中,可以在vue.config.js文件中配置代理:

module.exports = {
  devServer: {
    proxy: {
      '/api': {
        target: 'http://localhost:3000',
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      }
    }
  }
}

这段代码会将所有以/api开头的请求转发到http://localhost:3000,并将响应返回给前端。这样就可以绕过浏览器的同源策略限制了。  

  

标签:Origin,Control,跨域,requested,res,Access,Allow
From: https://www.cnblogs.com/mxx520/p/17440250.html

相关文章

  • 使用SpringMVC 拦截器导致出现@CrossOrigin失效问题解决办法
    非简单请求会发起一个OPTIONS方法的预检请求,这个请求会被拦截器拦截,但是服务器没有给浏览器返回必要的跨域指示信息(比如:“Access-Control-Allow-Origin”----允许哪些请求访问),浏览器没收到指示信息,就认为服务器不允许跨域请求,就会报错。所以需要在拦截器拦截OPTIONS方法的预......
  • How to Control an External USB Web Camera Using a Raspberry Pi All In One
    HowtoControlanExternalUSBWebCameraUsingaRaspberryPiAllInOne如何使用树莓派控制外接USB网络摄像头USB网络摄像头罗技C270i高清网络摄像头(视频120万像素,照片300万像素)1280x720=>720P1280*720//921600不足100万,90万✅PCh......
  • Mybatis-Plus自动生成代码,自定义Controller
    MP网址:https://baomidou.com/pages/779a6e/#%E4%BD%BF%E7%94%A8直接copy官网代码修改成自己的:privatevoidgenerate(){FastAutoGenerator.create("jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2b8","root","P@ss123.")......
  • 【老王读SpringMVC-5】Controller method 是如何执行的?
    通过前面对Controllermethod参数绑定的分析,我们知道,被@RequestMapping标记handlermethod的执行是通过调用RequestMappingHandlerAdapter#handle()。RequestMappingHandlerAdapter#handle()具体的调用过程如下:参数解析、handlermethod的执行和对返回值的处理,最终......
  • X-Requested-With
    原文:https://www.jianshu.com/p/2828d2d137e7x-requested-with的作用以及用法详解x-requested-with请求头区分ajax请求还是普通请求在服务器端判断request来自Ajax请求(异步)还是传统请求(同步): 两种请求在请求的Header不同,Ajax异步请求比传统的同步请求多了一个头参数......
  • 日常问题记录: HP LoadRunner Controller 已停止工作
    环境描述:系统:windowsserver压测工具:Loadrunner11现象描述:Controller在执行一段时间后崩溃,提示:HPLoadRunnerController已停止工作;根据并发用户多少执行时间基本成比例;例如12并发用户3小时,24并发用户1.5小时Windows提示信息:错误应用程序名称:wlrun.exe,版本:11.0.0.......
  • 隐藏GridControl每行的小加号
    GridView1.OptionsDetail.EnableMasterViewMode=False来源:C#怎么隐藏gridcontrol里面的小加号-CSDN社区......
  • iOS UITabBarController 典型应用
    -(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{self.window=[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];//Overridepointforcustomizationafterapplicationlau......
  • Nginx_启动时报错:Job for nginx.service failed because the control process exited
    一、报错如下Jobfornginx.servicefailedbecausethecontrolprocessexitedwitherrorcode.See"systemctlstatusnginx.service"and"journalctl-xe"fordetails.123二、解决原因1、先检查nginx配置文件正否正确输入nginx-t命令,如果反回successful表示配置文件......
  • controller是单例模式还是多例模式?spring默认的是单例模式,那么如何保证线程安全
    controller是单例模式还是多例模式在Java中,Controller既可以是单例模式,也可以是多例模式,这取决于具体的实现方式。在单例模式中,Controller只会被实例化一次,多个线程共享同一个实例。这样可以节约系统资源,提高系统性能。但是在多线程环境下,如果不加以保护,可能会出现线程安全的问......