1

I have the following code in my AngularJS4 project:

const url = "http://localhost:4151/test";
     this.http.get(url).toPromise()
         .then((r) => {
             console.log("SUCCESS", r);
         })
         .catch((e) => {
             console.error("ERR", e);
         });

The URL works fine when typed in the browser bar, and I have enabled CORS headers on the server side:

// Allow CORS
this.app.use(function(req: Request, res: Response, next: Function) {
   res.header("Access-Control-Allow-Origin", "*");
   res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
   next();
});

Whenever I make a call to the server from my Angular application, it returns a 404 - Not Found error.

Any ideas how to fix this?

Thanks.

2
  • 1
    Stab in the dark. (Side note at my current work I'm also drowning in prod CORS debugging :P). [a] Make sure your server is (actually) setting to allow requests from where your client is running, or all. [b] Can you hit the API endpoint directly? (eg not via the app) (just trying to rule out some things) Commented Dec 13, 2017 at 18:21
  • Hmm, try to set proxy.conf.json if you are using angular cli. DOCS Commented Dec 13, 2017 at 18:28

3 Answers 3

4

In case someone finds this question before the one below.

This was the problem: Angular4 giving 404 for json data that exists and is publicly serving

I had used the tutorial as a base, and was still using the HttpClientInMemoryWebApiModule to intercept http calls. I commented it out and it works fine.

...
// This needed to be commented out vvv
// import { HttpClientInMemoryWebApiModule } from "angular-in-memory-web-api";
// import { InMemoryDataService } from "./services/dataSvc/in-memory-data.service";

@NgModule({
    declarations: [
        AppComponent
    ],
    imports: [
        BrowserModule,
        FormsModule,
        MyRoutingModule,
        HttpClientModule,

        // The HttpClientInMemoryWebApiModule module intercepts HTTP requests
        // and returns simulated server responses.
        // Remove it when a real server is ready to receive requests.
        // This needed to be commented out vvv
        // HttpClientInMemoryWebApiModule.forRoot(
        //     InMemoryDataService, { dataEncapsulation: false }
        // )
    ],
    providers: [MessageService],
    bootstrap: [AppComponent]
})
export class AppModule { }
Sign up to request clarification or add additional context in comments.

Comments

1
HttpClientInMemoryWebApiModule.forRoot(
  InMemoryDataService, { dataEncapsulation: false }
)

Remove the above line from your app.module.ts file. I guess you put this in your project part of learning the httpclient from the documenta

Comments

0

For someone having the same problem, I fixed by adding "@ResponseBody" on my back-end spring RESTful method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.