0

I have an angular app with some i18n JSON files. The files work fine- the language of the site is changed perfectly depending on which one is selected. However, I get a funny console error which I can't figure out-

Uncaught SyntaxError: Unexpected token : at en.json: 2

Resource interpreted as Script but transferred with MIME type application/octet-stream

The same error appears in the console for each file.

Is there something wrong with the way my file is structured?

example of data in en.json:

{ 
    "WELCOME" : "Welcome",
    "HOME" : "Home",
    "GO" : "Go",
    "LOGOUT" : "Log Out"
}
2
  • Did you check the real JSON using a service like jsonlint.com? Commented Mar 17, 2014 at 13:46
  • I sure did- it says that it's valid. Commented Mar 18, 2014 at 9:35

1 Answer 1

1

There's nothing wrong with your JSON file but, according to the console log, it seems your web server does not set correct response header Content-Type which should be application/json. So your browser tries to interpret your file as a JavaScript file and find an invalid token.

For instance with Nginx, you could use mime type configuration file provided by HTML5-Boilerplate project (https://github.com/h5bp/server-configs-nginx/blob/master/mime.types) and include it into your Nginx configuration:

include /etc/nginx/mime.types;

It contains the following directive setting application/json as content type for JSON:

types {
  application/json                      json;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, can you tell me how I can set the correct response header?
@bookthief Which webserver do you use?
How do you serve your angular app (how do you launch it) or where is it hosted?
I have edited the answer with an example of mime type configuration with Nginx.

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.