1

i'm trying to check if a pdf file exists on the server with an HTTP get request,for that i am testing if the status code returned by the server's response is different then 200, the problem is that i always get status code 200 and statusText "OK".

here's my Service class

getPdf(year: number,type: number, num: number): boolean{
    this.http.get('http://localhost:4200/app/pdfs/'+year+'/'+type+'/'+num+'.pdf')
             .subscribe(data => console.log(data));
return true;
}

in my component i have this code :

ngOnInit(){
   this.pdfService.getPdf(2016,71015,1275);
}

and that's the result i get in Chrome dev tools console

enter image description here

any ideas of why that is happening and how i can check the existance of a file if this method is not applicable?

  • 6
    Looks like a server issue to me. Check the network tab in browser devtools if it reports a different response code there (or is the screenshot from the network tab already)? – Günter Zöchbauer Nov 23 '16 at 13:03
  • i don't think it's a server issue, here's what i get in the network tab: Request URL:http://localhost:4200/app/pdfs/2016/71015/1275.pdf Request Method:GET Status Code:304 Not Modified Remote Address:127.0.0.1:4200 – Yassine El Khanboubi Nov 23 '16 at 14:54

1 Answers1

3

I think your server is redirecting some or all requests for assets not found to your main page. It is somewhat common for a single page app. You need to update your server configurations to make it return a not found response for some folders at least.

Guilherme Meireles
  • 7,849
  • 2
  • 21
  • 15
  • Thank you for your quick reply, i forgot to mention that i use the web server that comes with angular 2 (started with ng serve), and i copied my projet folder to the root of another Apache server on ubuntu and still have the same issue, so i'm assuming that the redirection is made by angular and not the web server – Yassine El Khanboubi Nov 23 '16 at 14:14
  • Check the body in the response to see if the contents are the same as your index.html. If so then a redirect is happening. Else you need to take a look at your server configurations to see whats going on. Angular does not redirect your request to another asset. – Guilherme Meireles Nov 23 '16 at 14:34
  • 1
    yes exactly, apparently when the asset is not found it loads the index.html file without changing the url in the adress bar, the body of the response is actually the content of index.html – Yassine El Khanboubi Nov 23 '16 at 14:52