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).

    评论

报告相同问题?

悬赏问题

  • ¥25 大学xml问答,会的请回复
  • ¥15 小贝360-4 配二个 华772S 设置WⅰFi5G 连接
  • ¥15 vs2022的QT报错,好像是缺少winextras
  • ¥15 怎么看 cst中一个面的功率分布图
  • ¥15 c语言数据结构求9999
  • ¥15 Fiddler无法对部分小程序抓包
  • ¥60 Python代码 ip首部检验和计算代码 版本协议 首部长度 源地址 目的地址 存活时间
  • ¥18 微机原理汇编的综合实验
  • ¥15 LD衰减图用R语言对其可视化
  • ¥15 Mermaid语法生成的svg在Axure无法编辑