user6736489
user6736489

Reputation:

Angular $http using json file works but using json file with $httpBackend is not working to display data

I can fake getting data from json file with using $http but with $resource/ $httpBackend I don't get any results

$http.get (works)

 $http.get('api/devices.json')
      .then(function (result) {

      ...//

Above works just fine but the $httpBackend will work with json inline , but not pointing at JSON file

Controller file which calls the deviceResourceMock module

deviceResource.query(function(data) {    
       vm.devices = data; 
    });

deviceResourceMock module

app.run(function ($httpBackend) {

    var devices = 'test.json';  // Put new json file in same directory

    // ABOVE DOES NOT WORK 

This DOES work below though

var devices = {"Devices":  [
        {
            "DeviceId": 1,
            "DeviceStatus": "Leaf Rake",
            "id": "GDN-0011",
            "ha": "Leaf rake with 48-inch wooden handle.",
            "Urt": "blah"
        }
]};

URL and WhenGet

var deviceUrl = "/api/devices";
$httpBackend.whenGET(deviceUrl).respond(devices.Devices);

Thoughts on why it doesn't work?

Upvotes: 0

Views: 62

Answers (1)

Parwinder K
Parwinder K

Reputation: 13

Is "devices.Devices" function?

I think code should be like

$httpBackend.whenGET(deviceUrl).respond(function(method,url,data) {

return devices.Devices;

});

Upvotes: 0

Related Questions