代码之家  ›  专栏  ›  技术社区  ›  Ronaldo Lanhellas

使用ajax jquery的本地主机中的cors错误

  •  1
  • Ronaldo Lanhellas  · 技术社区  · 7 年前

    我有一个在localhost下运行的api,它接收post请求以创建帐户,因此我使用ajax jquery创建了一个简单的“html”页面来调用这个api,请参见:

    $.ajax({
                type: "POST",
                url: "http://localhost:8080/contas/",
                data: '{"email":"' + var_email + '"}',
                success: function () {
                    new Noty({text: 'Conta criada com sucesso', theme: 'relax', type: 'info'}).show()
                },
                dataType: "json"
            });
    

    但我总是

    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    

    我的api(spring boot)已经有了@crossorigin:

    @CrossOrigin(value = "*", origins = "*", maxAge = 3600, allowCredentials = "true", allowedHeaders = "*", methods =
            {RequestMethod.GET, RequestMethod.OPTIONS, RequestMethod.POST})
    

    但我认为如果请求来自同一个主机(localhost),则不需要这样做。

    0 回复  |  直到 7 年前