我们正在尝试在我们的测试环境中设置ServiceStack和CORS。当前没有设置IIS安全性(匿名)。尝试从客户端连接(响应)时,请求将通过
拒绝“从原点'https://xxx?format=json'到'http://localhost:3000'处获取的访问已被CORS策略阻止:对预检请求的响应未通过访问控制检查:没有'Access-Control-Allow-Origin'标头如果请求不透明,则可将请求的模式设置为“ no-cors”,以在禁用CORS的情况下获取资源。“
我们已经尝试了以下方法来设置ServiceStack,但不确定是服务器设置问题还是客户端设置问题。
Here's my request:
const https = require("https");
function deleteStaffMember(e) {
console.log(e.currentTarget.name)
try {
const res = fetch(`https://xxx/${e.currentTarget.name}?format=json`, {
method: 'DELETE',
agent: new https.Agent({
rejectUnauthorized: false
}),
credentials: 'omit'
})
} catch(err) {
console.log(err)
}
}
这里是AppHost服务堆栈的尝试,从当前尝试到之前的尝试:
public override void Configure(Container container)
{
//Permit modern browsers (e.g. Firefox) to allow sending of any HTTP Method
SetConfig(new HostConfig
{
GlobalResponseHeaders = {
{ "Access-Control-Allow-Origin", "*" },
{ "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS" },
{ "Access-Control-Allow-Headers", "Content-Type" },
},
});
}
先前:
Plugins.Add(new CorsFeature(
allowOriginWhitelist: *,
allowCredentials: false,
allowedHeaders: *));
甚至更早的时间:
Plugins.Add(new CorsFeature(
allowOriginWhitelist: new[] { "http://localhost","http://localhost:3000" },
allowCredentials: true,
allowedHeaders: "Content-Type, Allow, Authorization, X-Args"));
原始:
Plugins.Add(new CorsFeature());
编辑这是我的DELETE(我想这不会得到太多,因为这是飞行前的要求):
删除/ NDBServiceStack / staff / hierarchy / 1315来源:http://localhost:3000引荐来源:http://localhost:3000/index接受:/用户代理:Mozilla / 5.0(Macintosh; Intel Mac OS X 10_14_6)AppleWebKit / 605.1.15(KHTML,如Gecko)版本/13.0.5 Safari / 605.1.15
[如果您的请求需要Cookie,即依赖于身份验证会话,则必须将它们包含在JS fetch()
请求中,并且我还要指定mode以允许CORS请求,例如:
const res = fetch(`https://xxx/${e.currentTarget.name}?format=json`, {
method: 'DELETE',
agent: new https.Agent({
rejectUnauthorized: false
}),
mode: 'cors',
credentials: 'include'
})
您的Access-Control-Allow-Origin whitelist然后应明确包含您要允许的原点,描述中的原点可能是http://localhost:3000
:
Plugins.Add(new CorsFeature(
allowOriginWhitelist: new[] { "http://localhost:3000" },
allowCredentials: true,
allowedHeaders: "Content-Type, Allow, Authorization, X-Args"));