此错误是由于跨域资源共享中强加了限制。这已作为安全功能的一部分实现,以通过跨域调用来限制资源的客户端(域)。当您将请求发送到Web服务或api或类似工具时,它会在服务器或目标(此处是api)的请求中添加Origin标头,以验证请求是否来自授权来源。理想情况下,API
/服务器应
Origin在
Requestheader收到的信息中查找,并可能针对允许向其提供资源的一组来源(域)进行验证。如果来自允许的域,它将在响应标头中添加与
"Access-Control-Allow-Origin"值。也可以使用通配符,但是问题是,在获得通配符许可的情况下,任何人都可以发出请求并将其送达(有一些限制,例如通过Windows
auth或cookie对api进行身份验证,不允许您发送该
withCredentials值
*)
)。使用通配符来源的响应标头不是一个好习惯,因为它对所有人开放。
这些是使用值设置响应头的方法:-
Access-Control-Allow-Origin: *Access-Control-Allow-Origin: http://yourdomain.com
您甚至可以在同一响应中添加多个Access-Control-Allow-Origin标头(我相信在大多数浏览器中都可以使用)
Access-Control-Allow-Origin: http://yourdomain1.comAccess-Control-Allow-Origin: http://yourdomain2.comAccess-Control-Allow-Origin: http://yourdomain3.com
在服务器端(c#语法),您可以这样做:
var sourceDomain = Request.Headers["Origin"]; //This gives the origin domain for the request Response.AppendHeader("Access-Control-Allow-Origin", sourceDomain ); //Set the response header with the origin value after validation (if any) .Depending on the type of application you are using syntax may vary.希望这可以帮助!!!



