【发布时间】:2016-10-30 21:06:15
【问题描述】:
我有一个 owin/katana 项目。所以没有IIS。
public void Configuration(IAppBuilder app)
{
app.Run(context =>
{
context.Response.ContentType = "application/json";
context.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
// etc.
}
和客户端这个jQuery:
$.ajax({
dataType: "json",
url: labelPrintLoc,
success: function (msg) {
console.log(msg);
if (msg === "Done")
alert("Printed!");
else {
alert("Error, check the log on the server!");
}
},
error: function(a, b, c) {
console.log(a);
console.log(b);
console.log(c);
}
});
XMLHttpRequest 无法加载http://xxxx:9000 请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin http://yyy 是不允许访问的。
我见过类似的问题https://stackoverflow.com/a/6516634/169714,但没有AppendHeader 方法。我以为* 让每个人都可以访问?
edit 将从该网址尝试选项 3:https://researchaholic.com/2015/04/28/how-to-fix-no-access-control-allow-origin-header-in-asp-net-webapi/
edit2 添加 nuget 和这一行
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); 并删除带有 headers.add 的行给我一个 500 错误,因为原点已经设置。
【问题讨论】: