加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular – Http获取404 Not URL for URL:null

发布时间:2020-12-17 07:11:24 所属栏目:安全 来源:网络整理
导读:我试过像英雄之旅这样的示例应用程序. 我试图将服务cal转换为http get(InMemoryDbService),如下所示 hero.service.ts import {Injectable} from '@angular/core';import {HttpModule,Headers,Http} from '@angular/http';import 'rxjs/add/operator/toPromis
我试过像英雄之旅这样的示例应用程序.

我试图将服务cal转换为http get(InMemoryDbService),如下所示

hero.service.ts

import {Injectable} from '@angular/core';
import {HttpModule,Headers,Http} from '@angular/http';
import 'rxjs/add/operator/toPromise';

import {Hero} from './hero';

@Injectable()
export class HeroService {

    private heroesUrl = 'app/heroes'; 

    constructor(private http : Http) { }

    // working code
    //getHeroes(): Promise<Hero[]> {
    //    return Promise.resolve(HEROS);
    //}

    getHeroes(): Promise<Hero[]> {
        debugger;
        return this.http.get(this.heroesUrl).toPromise().then(responce => responce.json().data as Hero[]).catch(this.handleError);

    }
    private handleError(error: any): Promise<any> {
        console.error('An error occurred',error); // for demo purposes only
        return Promise.reject(error.message || error);
    }
}

内存-data.service.ts

import {InMemoryDbService} from 'angular-in-memory-web-api';

    export class InMemoryDataService implements InMemoryDbService {
    createDb() {
        let heroes = [
            { id: 1,name: 'Thulasi' },{ id: 2,name: 'Bala' },{ id: 3,{ id: 4,{ id: 5,{ id: 6,{ id: 7,{ id: 8,{ id: 9,{ id: 10,{ id: 11,name: 'Thulasi' }
        ];
        return heroes;
    }
}

抛出以下错误:

hero-service.ts:25 An error occurred Response_body: Object
    headers: Header
    sok: false
    status: 404
    statusText: "Not Found"
    type: null
    url: null
    __proto__: Body
HeroService.handleError @ hero-service.ts:25
    ZoneDelegate.invoke @ zone.js:232
    onInvoke @ core.umd.js:5975
    ZoneDelegate.invoke @ zone.js:231
    Zone.run @ zone.js:114
    (anonymous function) @ zone.js:502
    ZoneDelegate.invokeTask @ zone.js:265
    onInvokeTask @ core.umd.js:5966
    ZoneDelegate.invokeTask @ zone.js:264
    Zone.runTask @ zone.js:154
    drainMicroTaskQueue @ zone.js:401
    ZoneTask.invoke @ zone.js:339
    core.umd.js:2837 EXCEPTION: Uncaught (in promise): Response with status: 404 Not Found for URL: null

解决方法

我看到的一件事是,你的createDb()看起来不像原版..

它返回“集合”中的对象..

return {heroes};

我想lib需要这个对象来构建这些URL.

文件:https://github.com/angular/in-memory-web-api

英雄巡回赛:https://angular.io/docs/ts/latest/guide/server-communication.html#!#appendix-tour-of-heroes-in-memory-server

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读