Javascript AngularJS 和跨域请求被阻止
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25870199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS & Cross-Origin Request Blocked
提问by Muhammed AbdElFatah
any help please ? this error shows when i try to get data from server . my Angular App is in my local computer and the database in a server which i connect to it through internet.
有什么帮助吗?当我尝试从服务器获取数据时显示此错误。我的 Angular 应用程序位于我的本地计算机中,而数据库位于我通过互联网连接到它的服务器中。
error: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://XXXXXXXX.com/XXXXx. This can be fixed by moving the resource to the same domain or enabling CORS.
错误:跨域请求被阻止:同源策略不允许在http://XXXXXXXX.com/XXXXx读取远程资源。这可以通过将资源移动到同一域或启用 CORS 来解决。
what is the steps to solve this error ??
解决此错误的步骤是什么?
采纳答案by Karmasakshi Goyal
回答by iainjames9
Like what Hassene said, this is not due to AngularJS. Instead, the browser is blocking your JavaScript application from making a request across domain boundaries.
就像 Hassene 所说的,这不是由于 AngularJS。相反,浏览器会阻止您的 JavaScript 应用程序跨域边界发出请求。
To fix this, you should enable the Access-Control-Allow-Origin header in your server response. This is much easier than it sounds, and is documented for many different web servers here.
要解决此问题,您应该在服务器响应中启用 Access-Control-Allow-Origin 标头。这比听起来容易得多,并且在此处为许多不同的 Web 服务器进行了记录。
回答by Hassene Benammou
this is not related to Angular JS, your browser (probably chrome) is blocking cross origin requests you should configure your web server to rewrite local paths you are sending to it to your server paths. example nginx conf:
这与 Angular JS 无关,您的浏览器(可能是 chrome)正在阻止跨源请求,您应该配置您的 Web 服务器以将您发送给它的本地路径重写到您的服务器路径。nginx 配置示例:
location /local_path {
proxy_pass server_path
}
in your example:
在你的例子中:
location /homework {
proxy_pass http://XXXXXXXXXXXXX.com/XXXXXXX
}
回答by firiz
This is because in browsers like chrome, when you initiate a request from domain1.com to domain2.com, chrome automatically creates a OPTIONS request to check with the server wether domain1.com is allowed to send HTTP request to domain2.com or not.
这是因为在像 chrome 这样的浏览器中,当您从 domain1.com 向 domain2.com 发起请求时,chrome 会自动创建一个 OPTIONS 请求以检查服务器是否允许 domain1.com 向 domain2.com 发送 HTTP 请求。
According to your backend technology, you have to enable CORS (Cross-Origin Resource Sharing) in your server and configure it to allow domain1.com to access it.
根据您的后端技术,您必须在您的服务器中启用 CORS(跨域资源共享)并将其配置为允许 domain1.com 访问它。

