【发布时间】:2017-06-04 08:04:29
【问题描述】:
我正在 Angular 中进行服务调用,但它给了我错误
XMLHttpRequest cannot load http://geo.groupkt.com/ip/172.217.3.14/json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access
当我看到网络名称时,我找到了 json,当我看到详细信息时,我找到了响应
Request URL:http://geo.groupkt.com/ip/172.217.3.14/json
Request Method:GET
Status Code:200 OK
Remote Address:127.0.0.1:8888
Response Headers
view source
Age:1
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Encoding:gzip
Content-Length:232
Content-Type:application/json;charset=UTF-8
Date:Thu, 19 Jan 2017 14:51:51 GMT
Expires:0
Pragma:no-cache
Server:Apache-Coyote/1.1
Vary:Accept-Encoding
Via:1.1 localhost.localdomain
X-Content-Type-Options:nosniff
X-Frame-Options:DENY
X-XSS-Protection:1; mode=block
Request Headers
view source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8,hi;q=0.6,es;q=0.4
Host:geo.groupkt.com
Origin:http://localhost:3000
Proxy-Connection:keep-alive
Referer:http://localhost:3000/
User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
我现在怀疑即使网络发出呼叫,CORS 也会发生? 为什么会这样说,又是我调试代码的时候没能成功。
【问题讨论】:
-
CORS 是一种浏览器/客户端功能 - 因此即使您的请求得到正确解析,您的浏览器也会因为跨域策略而阻止对资源的访问。无论服务器
http://geo.groupkt.com/ip/172.217.3.14/json是谁 - 它不提供对 localhost:3000 进行调用的访问权限 - 最简单的解决方案是在运行 ng2 应用程序时使用代理 - 如果您使用 ng-cli :github.com/angular/angular-cli#proxy-to-backend
标签: javascript angular cors angular-services