我有一个在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),则不需要这样做。