Api 通过'访问XMLHttpRequest;https://xxxxxxxxx' 来自原点';http://localhost' 已经被封锁了
访问位于“”的XMLHttpRequesthttps://xxxxxxxxx“起源”http://localhost'已被CORS策略阻止:请求的资源上不存在'Access Control Allow Origin'标头。 我的代码: const headers=new HttpHeaders().set( “内容类型”, 'application/x-www-form-urlencoded;' ).set('Access-Control-Allow-Origin','*') 返回this.http.post(this.rootURL+'/connect/token', 身体 ,{'headers':this.headers})Api 通过'访问XMLHttpRequest;https://xxxxxxxxx' 来自原点';http://localhost' 已经被封锁了,api,angular11,Api,Angular11,访问位于“”的XMLHttpRequesthttps://xxxxxxxxx“起源”http://localhost'已被CORS策略阻止:请求的资源上不存在'Access Control Allow Origin'标头。 我的代码: const headers=new HttpHeaders().set( “内容类型”, 'application/x-www-form-urlencoded;' ).set('Access-Control-Allow-Origin','*') 返回this.ht
在生产中使用它欢迎使用堆栈溢出。出现此问题是因为您尝试访问另一个源,而不是提供应用程序的源(在您的情况下是localhost)。出于安全原因,浏览器会阻止此操作。有很多关于stackoverflow的答案,它们处理CORS问题。如果只是为了使用
ngserve
进行开发,您可以提供一个proxy-conf.json来映射https://.... 到子路径。像/api。我知道使用ng serve没有问题,但在生产中使用我的真实ip调用api出现在google chrome问题中,我想知道在生产中使用angular代码是否有任何解决方案。您无法从浏览器端绕过CORS阻塞。服务器需要允许来自另一个来源的请求(通过将access control alllow origin标头添加到响应中来实现),或者应用程序和服务器需要部署在同一域/IP地址下。对于这两种解决方案,您都需要更改服务器。