我使用ngResource在Amazon Web Services上调用REST API时收到此错误:
XMLHttpRequest无法加载 http://server.apiurl.com:8000/s/login?login=facebook.对预检请求的响应未通过访问控制检查:请求的资源上不存在"Access-Control-Allow-Origin"标头.原产地" :HTTP //本地主机,因此"是不允许访问. 错误405
服务:
socialMarkt.factory('loginService', ['$resource', function($resource){
var apiAddress = "http://server.apiurl.com:8000/s/login/";
return $resource(apiAddress, { login:"facebook", access_token: "@access_token" ,facebook_id: "@facebook_id" }, {
getUser: {method:'POST'}
});
}]);
Run Code Online (Sandbox Code Playgroud)
控制器:
[...]
loginService.getUser(JSON.stringify(fbObj)),
function(data){
console.log(data);
},
function(result) {
console.error('Error', result.status);
}
[...]
Run Code Online (Sandbox Code Playgroud)
我正在使用Chrome,我不知道还有什么可以解决这个问题.我甚至将服务器配置为接受来自源的头文件localhost.
我,我正在使用angular 6和asp.net核心上的SignalR功能.但不断收到此错误对预检请求的响应未通过访问控制检查:响应中的"Access-Control-Allow-Credentials"标头的值为'',当请求的凭据模式为'时,该值必须为'true'包括'.
做了一些研究,发现它是服务器端的CORS问题.所以修改了服务器代码.
startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(o => o.AddPolicy("CorsPolicy", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.WithOrigins("http://localhost:4200");
}));
services.AddSignalR();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseCors("CorsPolicy");
app.UseSignalR(routes =>
{
routes.MapHub<SignalR>("/rule");
});
}
Run Code Online (Sandbox Code Playgroud)
角应用
ngOnInit() {
this.callSignalR()
}
private callSignalR(): void {
// set up the signal R
let connection = new signalR.HubConnectionBuilder().withUrl(environment.baseUrl+"/rule").build();
//start the connection
connection.start().then(() => connection.invoke("updateRules", this.autheService.getCurrentuser.userId));
//Get the response from the server
connection.on("updateRules", data => {console.log(data);});
}
Run Code Online (Sandbox Code Playgroud)
引用