2017-03-14 2 views
-1

Ich möchte eine $http Anfrage machen Verwendung der Konfigurationsobjekt anstelle der schnellen Methode. Die Anforderung lautet "GET", und die URL zielt auf eine lokale Datei json ab.

Der Code sieht mehr oder weniger wie:

$http.get({ 
    method: 'GET', 
    url: 'data.json' 
    }).then(function(res) { 
    $scope.data = res; 
    }, function(res) { 
    console.log(res); 
    }) 

Der Fehler, den ich bekommen ist:

Error: [$http:badreq]

Hier ist ein 'Arbeits' plunker das Problem demonstriert.

Tatsache ist, dass, wenn ich die schnelle Methode verwenden $http.get('clients.json') es funktioniert.

Jede Hilfe wäre willkommen.

Antwort

1

Update Ihr app.js

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

app.controller('MainCtrl', function($scope, $http) { 
    $http({ 
    url: 'data.json', 
    type: 'GET' 
    }).then(function(res) { 
    $scope.data = JSON.stringify(res.data); 
    }, function(res) { 
    console.log(res); 
    }) 
}); 

DEMO

+0

Ich habe zweimal erwähnt, dass ** ich nicht die Shortcut-Methode ** verwenden möchte. Aber das Konfigurationsobjekt. – Korte

+1

Ich denke, Sie verwendet get type zweimal.Veränderung von $ http.get ({zu $ http ({ – Amal

+0

) Oh, jemand ist blind. Vielen Dank! – Korte

0

versuchen, dies es für mich funktioniert: plunker

app.controller('MainCtrl', function($scope, $http) { 
    $http({method: 'GET',url : './data.json'}).then(function(res) { 
    $scope.data = res; 
    }, function(res) { 
    console.log(res); 
    }) 
}); 
+0

Ich habe doppelt so genannten ** Ich möchte nicht die Verknüpfung Methode verwenden **. Aber das Konfigurationsobjekt. – Korte

+0

sorry @Korte sehe das Update –

0

erste Datei auf Ihrem index.html, Sie hatten {data}} statt:

<body ng-controller="MainCtrl"> 
    {{data}} 
</body> 

auf Ihrem Controller haben Sie die $http wie folgt (nicht $http.get verwenden, aber nur $http) aufzurufen:

$http({ 
    method: 'GET', 
    url: 'data.json' 
}).then(function successCallback(response) { 
    $scope.data = response.data; 
}, function errorCallback(response) { 
    console.log(response); 
}); 

Siehe Arbeits Update hier https://plnkr.co/edit/OCPkPYsbcHv2snef5Bj3?p=preview

0

scheint der Code falsch ist, die Sie bereits verwenden $ http.get, aber fügen Sie immer noch {method: 'GET'} hinzu.

Ich denke, Sie möchten $ http Anfrage direkt verwenden.

$http({ method: 'GET', url: 'data.json' }).then(function(res) { $scope.data = res; }, function(res) { console.log(res); })