Angular 2 HTTP GET返回URL null [英] Angular 2 HTTP GET returning URL null

查看:303
本文介绍了Angular 2 HTTP GET返回URL null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Typescript使用angular 2来创建一个简单的HTTP GET请求。我得到一个404错误,与空网址。

 从'@ angular / core'导入{Component,OnInit} ; 
从'@ angular / router'导入{Router};
从'./breadcrumbService'导入{BreadcrumbService};
从'@ angular / http'导入{Http,Response};
$ b $ @Component({
moduleId:module.id,
providers:[BreadcrumbService],
templateUrl:'html / appList.html',
styleUrls:['css / list.css']

})
export class HealthComponent {
构造函数(private http:Http){
this.http.get ('http://jsonplaceholder.typicode.com/posts/1')
.toPromise()
.then((res:Response)=> {
console.log(' RES(',res);
})
.catch((err:Error)=> {
console.log('ERR !!:',err);
});


$ / code $ / pre

错误信息:

 响应{_body:Object,status:404,ok:false,statusText:Not Found,headers:Headers ...} 
_body:Object
标题:标题
ok:false
status:404
statusText:Not Found
类型:2
url:null
__proto__ :Body


解决方案这可能是 InMemoryWebApiModule.forRoot 相关问题。当您加载内存中的api时,会发生这种情况,但尝试访问未定义的url。解决这个问题的方法是在 app.module config中设置 passThruUnknownUrl:true

InMemoryWebApiModule.forRoot(InMemoryDataService,{passThruUnknownUrl:true})..


I am trying to make a simple HTTP GET request using angular 2 with Typescript. I am getting a 404 error, with a null url. Shown below is my component file, and the error I am receiving.

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { BreadcrumbService } from './breadcrumbService';
import { Http, Response } from '@angular/http';

@Component({
  moduleId: module.id,
  providers: [ BreadcrumbService],
  templateUrl: 'html/appList.html',
  styleUrls: ['css/list.css']

})
export class HealthComponent {
  constructor(private http: Http) {
      this.http.get('http://jsonplaceholder.typicode.com/posts/1')
      .toPromise()
      .then((res: Response) => {
      console.log('RES: ', res);
      })
    .catch((err: Error) => {
      console.log('ERR!!: ', err);
    });
   }
}

The error message:

Response {_body: Object, status: 404, ok: false, statusText: "Not Found",     headers: Headers…}
 _body:Object
 headers:Headers 
 ok:false
 status:404
 statusText:"Not Found"
 type:2
 url:null
 __proto__:Body

解决方案

This is probably InMemoryWebApiModule.forRoot related issue. This happens when you load in-memory api, but trying to reach undefined url. The way to solve this is by setting passThruUnknownUrl: true in app.module config:

InMemoryWebApiModule.forRoot(InMemoryDataService, {passThruUnknownUrl: true}) ..

这篇关于Angular 2 HTTP GET返回URL null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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