2017-03-14 44 views
-1

我想使$http請求利用配置對象而不是快速方法。該請求是'GET'方法,並且該URL針對的是本地文件json

代碼看起來多少有點像:

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

我得到的錯誤是:

Error: [$http:badreq]

這裏的 '工作' plunker證明的問題。

事實是,如果我使用快速方法$http.get('clients.json')它的作品。

任何幫助,將不勝感激。

回答

1

更新您的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

我已經提到過兩次**我不想使用快捷方式**。但是配置對象。 – Korte

+1

我想你用get類型twice.change從$ http.get獲得({to $ http({ – Amal

+0

哦,有人是盲人,非常感謝! – Korte

0

試試這個工作對我來說: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

我已經提到的兩倍**我不想使用快捷方法**。但是配置對象。 – Korte

+0

抱歉@Korte查看更新 –

0

首先您index.html,你有{data}}代替:

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

在你的控制,你必須調用$http如下(不使用$http.get只是$http):

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

見工作更新這裏https://plnkr.co/edit/OCPkPYsbcHv2snef5Bj3?p=preview

0

看來你的代碼是錯誤的,你已經在使用$ http.get,但仍然添加{方法:'GET'}。

我想你想直接使用$ http請求。

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