【发布时间】:2017-02-01 21:44:17
【问题描述】:
我有这个代码:
客户端(angular2)托管在此网址上:
http://localhost:8080/Me/html/pages/gasolina/country/USA
代码:
return this.http
.post(this.supporToolSaveUrl, gasConfigModel, {headers: headers})
.map(res => res.json())
.subscribe(
data => {
console.info("next: ");
console.info(data)
},
err => console.error(err)
);
服务器(java jersey)
在此网址上托管:
http://localhost:8080/Me/services/countries
代码:
@Path("/SaveConfig")
@POST
@Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public void saveConfig(CountryGasStationConfig countryGasStationConfig) throws Exception {
我应该在球衣注释中添加这些吗?
当我在本地运行时出现此错误:
MLHttpRequest cannot load http://locahost:8080/.... Response for preflight is invalid (redirect)
我看到了这个post 和这个帖子
如何在 angular2 中制作这个解决方案?
应用程序。
config(['$httpProvider', function ($httpProvider) {
//Reset headers to avoid OPTIONS request (aka preflight)
$httpProvider.defaults.headers.common = {};
$httpProvider.defaults.headers.post = {};
$httpProvider.defaults.headers.put = {};
$httpProvider.defaults.headers.patch = {};
}]);
【问题讨论】:
-
如果您控制服务器,为什么不在服务器上正确处理 CORS,而不是尝试求助于一些客户端黑客。
-
我不想让所有网站都使用 CROS,你建议如何在服务器端解决这个问题?客户端和服务器共享同一个域,应该不会抛出 CROS 错误
标签: javascript angularjs http angular typescript