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

问题描述

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

 这可能是 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 04:18