11

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 || { };
}
Claies
  • 22,124
  • 4
  • 53
  • 77
Asif
  • 479
  • 2
  • 6
  • 12
  • Not sure but try to do it if it works.. Chang> `Observable to Observable` and `return body.data|| {} to return body||{}` – micronyks Jul 10 '16 at 00:00
  • No luck, still getting the same exception. I believe this is something to do with the response which I'm getting from server – Asif Jul 10 '16 at 09:39

3 Answers3

4

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

mansoor.khan
  • 2,309
  • 26
  • 39
4

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

kenadet
  • 245
  • 5
  • 13
2

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

Asif
  • 479
  • 2
  • 6
  • 12
  • Where is 'Api'? What libary should I import ? – jeongmin.cha Oct 29 '16 at 09:06
  • You need to import it from flask-restful package like this `from flask_restful import Api`, you can check the complete source code here https://github.com/asifpy/inventory-manager – Asif Oct 30 '16 at 05:51
  • Buddy, u said in question that you're trying to access your own server but no success you used cors, why? Did you implemented to block cross origin request on your own server? Buddy i made my own server api(mean application)with express but getting same error, no luck – blackHawk Dec 14 '16 at 16:42