gpt4 book ai didi

angular - 错误 : Uncaught (in promise): Response with status: 404 Not Found for URL in angular 2

转载 作者:太空狗 更新时间:2023-10-29 19:36:25 25 4
gpt4 key购买 nike

我有如下服务

    return this._http
.post('/app/php/reports.php', data.toString(),
{headers: this.headers})
.toPromise()
.then(res => res.json().data)
.catch(this.handleError);

我正在将我的代码从普通 JS 转换为 Angular。它可以正常使用普通的 ajax 调用。但我无法使用 angular2 发布。有人可以建议我,为什么它不调用 .php

进口的东西是

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

header定义如下

private headers = new Headers({'Content-Type': 'application/json'});

如果我在控制台错误中单击 URL,它会下载 php 文件。这意味着 URL 是正确的。

最佳答案

如果您在“app.module.ts”@NgModule 导入中使用 InMemoryWeApiModule.forRoot(InMemoryDataService),请尝试将其更改为:

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

Pass thru to a live XHRBackend If an existing, running remote server should handle requests for collections that are not in the in-memory database, set Config.passThruUnknownUrl: true. This service will forward unrecognized requests via a base version of the Angular XHRBackend. https://github.com/angular/in-memory-web-api#pass-thru-to-a-live-xhrbackend

app.module.ts
...

// Imports for loading & configuring the in-memory web api

import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
import { InMemoryDataService } from './in-memory-data.service';

@NgModule({
imports: [
...
InMemoryWebApiModule.forRoot(InMemoryDataService, {passThruUnknownUrl: true}), // fake in memory API simulation
],
...

关于angular - 错误 : Uncaught (in promise): Response with status: 404 Not Found for URL in angular 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39630806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com