这是我的JSON数据 Content.json
[{
status: "Allocated",
count: 45
}, {
status: "Bench",
count: 89
}, {
status: "Mobile",
count: 12
}, {
status: "Project",
count: 1
}, {
status: "SAP",
count: 18
}, {
status: "Testing",
count: 68
}, {
status: "vvvv",
count: 70
}];
尝试获取该JSON文件
angular.module('myApp')
.controller('valueController', ['$scope', '$http', '$window', function ($scope, $http, $window) {
var mainInfo = null;
$http({
method : "GET",
url : "/JSON/Content.json",
dataType: 'json',
}).then(function mySucces(response) {
$scope.mainInfo = response.data;
$window.alert(response.data);
}, function myError(response)
{
$window.alert(response.statusText);
});
$scope.chartOptions = {
dataSource: mainInfo,
series: {
argumentField: "status",
valueField: "count",
name: "SIDG Java",
type: "bar",
color: '#1899dd'
}
};
}]);
警觉我得到了:
Localhost:8080说:
[{
status: "Allocated",
count: 45
}, {
status: "Bench",
count: 89
}, {
status: "Mobile",
count: 12
}, {
status: "Project",
count: 1
}, {
status: "SAP",
count: 18
}, {
status: "Testing",
count: 68
}, {
status: "vvvv",
count: 70
}];
我期待的是
Localhost:8080说:
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],
答案 0 :(得分:0)
AngularJS可能无法在响应数据中检测到JSON。可能是响应中的Content-Type
标头不是application/json
?
您可以尝试使用dataType: 'json'
替换$http
配置中的responseType: 'json'
,告诉AngularJS预期JSON响应。
答案 1 :(得分:0)
我通过实现您的代码在警报中获取[对象,对象] [对象,对象] [对象,对象]。请再次检查JSON。
如果需要[object,object]结构,可以通过 JSON.parse(response.data)解析数据
答案 2 :(得分:-2)
要在警报中显示,请使用JSON.stringify函数将其转换为json字符串。