我在春季啓動時遇到了CORS問題。我已經配置好了這樣的CORSSpring Boot和CORS
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**");
}
}
我想可以啓用所有頭文件和其他東西。
它可以很好地與GET請求
$.get("someUrl, function(data, status){
console.log(data[0].latitude);
});
但每當我做出這樣的
$.ajax({
url: 'someUrl',
type: 'post',
dataType: 'json',
crossDomain: true,
contentType: "application/json; charset=utf-8",
success: function (data) {
console.log(data);
},
data: object
});
我得到POST請求以下
OPTIONS XHR "someUrl" [HTTP/1.1 403 Forbidden 4ms]
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at "someUrl".
(Reason: CORS header 'Access-Control-Allow-Origin' missing).
我怎樣才能解決這個問題呢?
不管是什麼引起的問題,我不認爲這是您曾經參與過這個問題的片段中。我只是用最少量的代碼在全新的Spring Boot(1.3.2。)設置中嘗試了它,並且在我從其他域POST'時正確添加了CORS頭。你使用什麼版本的Boot,從你的用戶代理那裏「發佈」你的請求?你的項目中的其他依賴項可能會覆蓋你的Cors設置(或者在控制器上更明確的配置?)? – sthzg
我使用Spring Boot 1.3.0.RELEASE和用戶代理是Firefox。實際上它是重定向到其他彈簧引導應用程序的其他端點的Api網關應用程序。我使用netflix的Zuul。 – nurgasemetey