1

I am trting to read a json file through Angular's built-in services called $http.But, the page gives me an error saying "Error: Failed to execute 'send' on 'XMLHttpRequest': Failed to load respective json file".

Here's my code:

angularExample.html

<!DOCTYPE html>
<html ng-app="Fino">
  <head>
    <link rel="stylesheet" type="text/css" href="scripts/bootstrap.min.css" />
    <script type="text/javascript" src="scripts/angular.min.js"></script>
    <script type="text/javascript" src="scripts/app.js"></script>
  </head>
  <body ng-controller="FinController as fin">

  <ul ng-repeat="emp in fin.employees" >
  <li>Name:{{emp.name}}</li>
  <li>Age:{{emp.age}}</li> 
  </ul> 

  </body>
</html>

app.js

var app=angular.module("Fino",[]);

app.controller('FinController',['$http',function($http){
    var store=this;
    $http.get('emp.json').success(function(data){
        store.employees=data;
    });
}]);

emp.json

[

            {name:'abc',age:27},
            {name:'bcd',age:24},
            {name:'efg',age:22}
    ];

Error Snapshot:

enter image description here

2
  • Please specify the proper URL for the file inside get. Commented May 4, 2015 at 13:04
  • Aha. You need a webserver ( like apache or http-server if you use node) to serve your files instead of directly trying to access it using file protocol. Commented May 4, 2015 at 13:10

1 Answer 1

2

As @Puneetsri said, the best option is to host your files on a web server. Otherwise try launching your app in chrome after starting it with the following flags

chrome.exe --allow-file-access-from-files 
Sign up to request clarification or add additional context in comments.

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.