raj m
raj m

Reputation: 2023

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

I have a service like below

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

I am converting my code from normal JS to angular. It is working fine with normal ajax call. But I coulnt post using angular2. Can somebody please suggest me, why its not calling .php

imported things are

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

header is defined below

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

If I click the URL in in console error it download the php file. That means URL is correct.

Upvotes: 2

Views: 7626

Answers (1)

Matty Matt
Matty Matt

Reputation: 161

If you are using the InMemoryWeApiModule.forRoot(InMemoryDataService) in your 'app.module.ts' @NgModule imports, try changing it to:

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
  ],
  ...

Upvotes: 16

Related Questions