角JS使用$ HTTP()得到响应,状态200,但数据为空 [英] Angular JS using $http() get a response, with status 200, but data is null
本文介绍了角JS使用$ HTTP()得到响应,状态200,但数据为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用这个角度code:
I'm using this angular code:
$http({
method: 'get',
url: 'json/test.json',
responseType: "json"
}).success(function(data, status, headers, config) {
console.log('Success: ' + data);
$scope.tests = data;
}).error(function(data, status, headers, config) {
console.log('Error: ' + data);
});
回应是这样的:
[{
id: 1,
name: 'aaaa',
age: 20
}, {
id: 2,
name: 'bbbbb',
age: 21
}]
在控制台,它打印:
Success: null
怎么来它返回成功,但数据为null。
how come it returns success, but data is null.
任何帮助AP preciated!
any help appreciated!
推荐答案
这可能会发生,因为 JSON
的格式不正确。尝试:
This might happen because json
is not formatted properly. Try:
[
{
"id": 1,
"name": "aaaa",
"age": 20
},
{
"id": 2,
"name": "bbbbb",
"age": 21
}
]
和使用
$http.get("json/test.json").success(function (data) {
console.log('success: ' + data)
});
或者你可以使用你原来的code,但没有的responseType
作为@DeborahK想通了:
or you may use your original code but without responseType
as @DeborahK figured out:
$http({
method: 'get',
url: 'json/test.json'
}).success(function(data, status, headers, config) {
console.log('Success: ' + data);
})
但无论如何,你需要格式化你的 JSON
得当,否则就会角度分析提高异常。
But anyway you would need to format your json
properly, otherwise angular will raise parsing exception.
这篇关于角JS使用$ HTTP()得到响应,状态200,但数据为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文