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

*爱你&永不变心* 提交于 2019-12-18 05:48:12

问题


I'm getting following exception, when I'm trying to connect to remote API using anguar2 http. Also my web server is receiving request and responding properly.

I'm able to make a successful curl request to the local server.

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 || { };
}

回答1:


I had the same error with a MEAN2 app. All I had to do was install the CORS middleware and use it with express.




回答2:


Server unavailable issue, might be CORS or server went down.




回答3:


Hope this will helps someone.

The problem was with resource definition. When I'm trying to connect to remote resource using angular2 http.get, I was getting following error

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at …

FIX:

As I'm using flask-restful to build remote API, below fix solved my problem

from flask_restful.utils import cors
from flask_restful import Api

api = Api(app, decorators=[cors.crossdomain(origin='*')])

Complete source code will be available here : inventory-manager



来源:https://stackoverflow.com/questions/38286250/exception-response-with-status-0-for-url-null-in-angular2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!