EXCEPTION:状态响应:URL为0:angular2为null

Asi*_*sif 11 flask flask-restful angular

当我尝试使用anguar2 http连接到远程API时,我遇到了异常.我的Web服务器也正在接收请求并正确响应.

我能够向本地服务器发出成功的curl请求.

EXCEPTION: Response with status: 0 for URL: null
Run Code Online (Sandbox Code Playgroud)

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 || { };
}
Run Code Online (Sandbox Code Playgroud)

Asi*_*sif 2

希望这会对某人有所帮助。

\n\n

问题出在资源定义上。当我尝试使用 angular2 连接到远程资源时http.get,出现以下错误

\n\n
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at \xe2\x80\xa6\n
Run Code Online (Sandbox Code Playgroud)\n\n

使固定:

\n\n

当我使用flask-restful构建远程 API 时,下面的修复解决了我的问题

\n\n
from flask_restful.utils import cors\nfrom flask_restful import Api\n\napi = Api(app, decorators=[cors.crossdomain(origin=\'*\')])\n
Run Code Online (Sandbox Code Playgroud)\n\n

完整的源代码将在此处提供:库存管理器

\n