EXCEPTION:响应状态:0表示URL:null表示angular2 [英] EXCEPTION: Response with status: 0 for URL: null in angular2

查看:238
本文介绍了EXCEPTION:响应状态:0表示URL:null表示angular2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我可以向本地服务器发出成功的curl请求。

  EXCEPTION:状态为0的响应: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 连接到远程资源时,出现以下错误:

<$ p $


$ b $ 跨源请求被阻止:同源策略不允许读取远程资源。 b

FIX:

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

  from flask_restful.utils import cors $ b $ from flask_restful import Api 

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

代码将在这里可用:


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

解决方案

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

这篇关于EXCEPTION:响应状态:0表示URL:null表示angular2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆