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 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧