$ http.get from json file always returns 404

I want to add a JSON configuration file to my application.
I added it to my project and tried to get it using $ http.get:

$http.get('http://localhost/myProject/content.json').success(function (data) {
        // Do stuff...
}).error((data, status, headers, config) => {
        // Failure...
});

The problem is that every time I get a 404 error.

+4
source share
3 answers

This is a configuration problem with the mime type of your web server - it doesn't have json, probably. Try renaming the file extension to .txt or .html and it should work.

You can also add a mime extension to the server. For IIS express, this is web.config. For instance:

 <staticContent>
    <remove fileExtension=".json" />
    <mimeMap fileExtension=".json" mimeType="application/json" />
  </staticContent>
+5
source

put the content.json sibling file in index.html and use this:

$http.get('content.json').success(function (data) {
        // Do stuff...
}).error((data, status, headers, config) => {
        // Failure...
});
0
source
<configuration>
    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>
   **<system.webServer>
    <staticContent>
      <mimeMap fileExtension=".json" mimeType="application/json" />
    </staticContent>
  </system.webServer>**
</configuration>


This works fine.. as what Erez A. Korn has said.
0

All Articles