http - Angular 2 HTTP GET 返回 URL null

标签 http angular get

我正在尝试使用 angular 2 和 Typescript 发出一个简单的 HTTP GET 请求。我收到 404 错误,网址为空。下面显示的是我的组件文件,以及我收到的错误。

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

错误信息:

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

最佳答案

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

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

关于http - Angular 2 HTTP GET 返回 URL null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39650686/

相关文章:

spring - 为什么 SessionFixationProtectionStrategy 不迁移 session 属性?

javascript - 根据 id 重置特定超时

php - 如何使用 codeigniter uri 格式创建搜索表单?

node.js - require 中的 Node js : Why module. Exports = tesdf() 仅在 get 请求中执行一次

node.js - 如何在 Node 中通过 HTTP 读取二进制文件?

java httpSession 在处理时失效

http - 为什么主机地址包含在 HTTP 1.1 GET 命令中?

javascript - 如何通过循环将属性推送到数组内对象内的数组?

angular - 带有 [(ngModel)] 的硬编码字符串

关于 GET 和 sprintf 的 C++ 代码