![](/img/trans.png)
[英]How to automatically parse spring form data to json object? (Spring, jQuery, AJAX, JSON)
[英]How can I parse AJAX Json data in object form in angularJS
這是我的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],
AngularJS可能無法以某種方式檢測到響應數據中的JSON。 響應中的Content-Type
標頭可能不是application/json
嗎?
您可以嘗試將$http
配置中的dataType: 'json'
替換為responseType: 'json'
以告訴AngularJS預期JSON響應。
通過實現您的代碼,我在警報中得到了[object,object] [object,object] [object,object]。 請再次檢查JSON。
如果需要[object,object]結構,則可以通過JSON.parse(response.data)解析數據。
要顯示在警報中,請使用JSON.stringify函數將其轉換為json字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.