当我尝试使用anguar2 http连接到远程API时,我得到了以下异常。我的Web服务器也正在接收请求并正确响应。
我能够向本地服务器发出成功的curl请求。
EXCEPTION: Response with status: 0 for URL: null
service.ts
getAllProducts(): Observable<string> {
return this.http.get(this.productUrl)
.map(this.extractData)
}
private extractData(res: Response) {
let body = res.json();
console.log(body)
return body.data || { };
}
我在MEAN2应用程序中遇到了同样的错误。我所要做的就是安装 CORS 中间件并与express一起使用。
服务器不可用的问题,可能是CORS或服务器崩溃了。
希望这会对某人有所帮助。
问题在于资源定义。当我尝试使用angular2连接到远程资源时 http.get
,我得到了以下错误
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at …
固定:
因为我正在使用 flask-restful
为了构建远程API,下面的修复解决了我的问题
from flask_restful.utils import cors
from flask_restful import Api
api = Api(app, decorators=[cors.crossdomain(origin='*')])
完整的源代码将在这里提供: 库存管理