异常:状态响应:URL 为 0:angular2 中为空

EXCEPTION: Response with status: 0 for URL: null in angular2

当我尝试使用 anguar2 http 连接到远程 API 时,出现以下异常。我的网络服务器也在接收请求并正确响应。

我能够向本地服务器发出成功的 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 || { };
}

希望这对某人有所帮助。

问题出在资源定义上。当我尝试使用 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='*')])

完整的源代码将在此处提供:inventory-manager

我在 MEAN2 应用程序中遇到了同样的错误。我所要做的就是安装 CORS 中间件并将其与 express 一起使用。

服务器不可用问题,可能是 CORS 或服务器宕机。