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 无法加载以下来源的扩展程序
    • ¥30 关于#oracle soa#的问题,请各位专家解答!
    • ¥15 mounriver里怎么对电机初始化是对pwm初始化函数和gpio初始化函数吗
    • ¥15 anaconda第一步就出不来了,文件名错误
    • ¥15 这个排列组合问题的思路哪个地方有错
    • ¥15 首页运行报错,首页运行报错
    • ¥15 jupyter里pandas为什么调用不了
    • ¥15 解决neuralprophet库运行报错
    • ¥20 想通过nas 配置一台iis服务器
    • ¥30 利用python编写元胞自动机教室火灾出逃,计算最佳出逃时间