helloxielan 2018-02-02 07:41 采纳率: 0%
浏览 50

jQuery Spring Boot,CORS问题

My front end pages are developed whit jquery 3.2.1, running by npm like this

"start": "http-server -a localhost -p 8000 -P http://localhost:8080 -c-1 --cors ./app"

my http request example:

$(function(){

        $.ajax({
            url: 'http://localhost:8080/api/partner/all',
            type:'get',
            dataType:'text',
            success:function(data){
                alert(data);
            }
        });
    }
)

and I get the cors error, like this

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access. The response had HTTP status code 403.

then I add the filter for cors supports

response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Max-Age", "1728000");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

now only one error is:

GET http://localhost:8080/api/partner/all 403 ()

How can I fix this problem?

  • 写回答

1条回答 默认 最新

  • weixin_33719619 2018-02-02 23:10
    关注

    Access-Control-Allow-Origin: * and Access-Control-Allow-Credentials: true are incompatible.

    If you need to pass Access-Control-Allow-Credentials: true (because you require cookie/authentication support) you will have to specify Access-Control-Allow-Origin: {value-of-Origin-request-header} (in your example, you could hardcode it to be Access-Control-Allow-Origin: http://localhost:8000).

    评论

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd