Skip to content

Page 31: Serving JSON up to Firefox #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
neeklamy opened this issue Sep 3, 2016 · 1 comment
Open

Page 31: Serving JSON up to Firefox #6

neeklamy opened this issue Sep 3, 2016 · 1 comment

Comments

@neeklamy
Copy link

neeklamy commented Sep 3, 2016

The Problem

Firefox has a problem with correctly receiving the JSON formatted data, the issue is caused by the request headers that it sends along. The default:
Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"

Google Chrome by contrast sends this request header, which accepts anything:

Accept: */*

So, if you’re using Firefox, when you get to this point in the book, where the list of books is expected, nothing appears. Nothing appears because ng server has sent back the main index.html instead of the desired JSON in response, viewing Firefox’s developer tools console shows this error:

EXCEPTION: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

(The first and unexpected character is the first character <.)

The Fix

One way to fix this is to change the Header sent with the http.get.

Before:

// src/app/book-list/book-list.component.ts
[…]
import { Http } from '@angular/http';
[…]

  ngOnInit() {
    this.http.get('/api/books.json')
      .subscribe((response) => this.books = response.json());
  }

[…]

After:

// src/app/book-list/book-list.component.ts
[…]
import { Http, Headers } from '@angular/http';
[…]

  ngOnInit() {
    this.http.get('/api/books.json', { headers: new Headers({'Accept' : '*/*' }) })
      .subscribe((response) => this.books = response.json());
  }

[…]

Maybe there’s a better way? Maybe this will be fixed in Angular?
Source: angular/angular-cli#889

@jasonswett
Copy link
Owner

Hey @neeklamy, I just want to acknowledge that I'm seeing these issues you've submitted as say thanks for the help. I'll be going through and addressing all these soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants