2016-09-22 32 views
2

找不到URL我有像下面錯誤:未捕獲(在承諾):與狀態響應:404在角2

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

我轉換我的代碼從正常JS到角的服務。它正常工作正常的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'; 

頭被定義如下

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

如果我在控制檯錯誤點擊URL它下載PHP文件。這意味着URL是正確的。

+0

你正在哪裏哪個錯誤? – ranakrunal9

+0

@ ranakrunal9他得到404錯誤,正如問題標題 – candidJ

+0

中提到的那樣,您正在獲取404表示帶有角度2請求API的API,但無法訪問。檢查並比較你的URL與正常和角度2請求。可能是你缺少任何標題或任何其他設置 – ranakrunal9

回答

16

如果你正在使用你的 '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 
    ], 
    ... 
相關問題