背景

刚刚上线了一个服务,其他客户需要在跨域情况下对于服务进行调用,几次尝试之后,终于成功调用了。本文解决 nginx + spring boot + juery 情况下的跨域处理

操作如下

使用nginx配置好以下内容:

server {listen 80;server_name xxx.com;location /data/ {proxy_pass 转发地址;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;add_header Access-Control-Allow-Origin *;add_header Access-Control-Allow-Methods GET,POST,OPTIONS;add_header Access-Control-Allow-Headers X-Requested-With;add_header Access-Control-Max-Age 60000;add_header Access-Control-Allow-Credentials true}
}

正常情况下上面的配置即可 完成服务的跨域配置。由于我是在jquery语言下进行处理,所以存在如下异常:

jquery 请求

$.ajax({type : "POST",url : "请求地址",dataType: "json",data : param,contentType:"application/json;charset=utf-8",success : function(data) {alert("成功");}
});

运行结果


描述

我们注意到 这里是请求失败了,同时出现两次请求 一次为OPTIONS方法的请求,一次为引荐来源网址政策: strict-origin-when-cross-origin。说明由于OPTIONS请求失败,导致不能得到正确的响应结果

处理

nginx配置文件中继续修改如下内容,让OPTIONS请求成功

server {listen 80;server_name xxx.com;location /data/ {proxy_pass 转发地址;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;add_header Access-Control-Allow-Origin *;add_header Access-Control-Allow-Methods GET,POST,OPTIONS;add_header Access-Control-Allow-Headers X-Requested-With;add_header Access-Control-Max-Age 60000;add_header Access-Control-Allow-Credentials true;if ( $request_method = OPTIONS ){return 200;}}
}

strict-origin-when-cross-origin 403 异常解决相关推荐

  1. Jetty Cross Origin Filter解决jQuery Ajax跨域访问的方法

    当使用jQuery Ajax post请求时可能会遇到类似这样的错误提示 XMLHttpRequest cannot load http://xxxxxx. Origin http://xxxxxx ...

  2. 解决Cross origin requests are only supported for protocol schemes问题

    解决Cross origin requests are only supported for protocol schemes问题 问题描述 本前端小白在学习的时候尝试按照一个网上的blog的代码来跑 ...

  3. 【跨域异常】Cross origin requests are only supported for protocol schemes: http, data, chrome,chrome-exten

    一.背景描述 下面是我用ajax异步请求时的写法,请注意url是怎么写的,下面的错误就是由于这个url的写法导致的. // 获取省份列表 $(document).ready(function () { ...

  4. Access to script at ‘xxx‘ from origin ‘null‘ has been blocked by CORS policy: Cross origin requests

    前言 本地调试js时,遇到本错误 Google Chrome 84.0.4147.135 (正式版本) (64 位) (cohort: Stable Installs Only) 修订版本 c42bd ...

  5. 【chrome错误】Cross origin requests are only supported for protocol schemes: http, data,chrome-extension

    使用ajax请求本地文件,chrome会报跨域错误:XMLHttpRequest cannot loadfile:///C:/Users/Li/Desktop/images/alist.json.Cr ...

  6. ajax跨域请求问题:Access to XMLHttpRequest at……has been blocked by CORS policy: Cross origin requests

    背景:本地代码调试时,加载本地资源正常,jquery的AJAX请求服务端资源报错. Access to XMLHttpRequest at '***** ' from origin 'null' ha ...

  7. Safari浏览器解除cross origin restrictions限制

    在写前端绘制中国地图的时候,需要调用Json文件,在本地调试的时候遇到如下报错: 开发环境:macOS 系统 safari浏览器 报错如下: XMLHttpRequest cannot load fi ...

  8. [NodeJS] Jest 环境下 Axios 请求报错: Cross origin http://localhost forbidden

    文章目录 问题概述 解决方案 参考 本博客站点已全量迁移至 DevDengChao 的博客 https://blog.dengchao.fun , 后续的新内容将优先在自建博客站进行发布, 欢迎大家访 ...

  9. 【VSCode】from origin ‘null‘ has been blocked by CORS policy: Cross origin requests are only supported

    Access to script at 'file:///C:/Users/dawulei/Desktop/%E9%A1%B9%E7%9B%AE/%E5%9D%A6%E5%85%8B%E5%A4%A7 ...

最新文章

  1. python练习_Python随笔31:Python基础编程练习题27~28
  2. Response.Write详细介绍
  3. 成功解决CondaError: Error reading file, file should be a text file containing packages conda create --he
  4. 使用java多线程分批处理数据工具类
  5. android电梯程序设计,课内资源 - 基于Android实现的电梯调度模拟
  6. c调用python代码找不到模块_构建 Python C 扩展模块
  7. tocmat类加载:正统的类加载
  8. VB6基本数据库应用(一):数据库基础
  9. 苹果ID申请开发者 双重认证问题?
  10. 我们为什么要使用空接口
  11. npm run build:具体
  12. 一个不错的SQLserver 数据库比较软件
  13. Java—九九乘法表
  14. Java集成ElasticSearch及配置类工具类整理
  15. 电子电路笔记----基本放大电路--半导体三极管
  16. 网络流的各种加边优化
  17. 趣味数学:解24点游戏小技巧
  18. 哪些场景下使用MongoDB
  19. 微软面试100题2010年版全部答案集锦(含下载地址)
  20. AWS亚马逊实战-(移动端直传S3)服务器端调用AWS STS生成用户临时凭证上传至S3

热门文章

  1. 如何实现echarts树点击节点名称也可以实现展开收起(直接更改options即可)
  2. 区块链与价值互联网的未来发展
  3. stm32单片机控制好盈电调带动T80电机转动
  4. NOIP2012 开车旅行
  5. 射线与AABB相交检测
  6. 土豆数据霍向琦:让时空信息像水电一样服务数字经济
  7. 影视制作设备/软件管理
  8. 一 树莓派简介与购买
  9. winPE无法识别SSD的解决方案(关闭VMD)
  10. 【STM32F10x串口通信控制LED输出模式以及亮灭】