【发布时间】:2016-12-05 00:12:49
【问题描述】:
我正在从 angularjs 1.5x 客户端向 Java HTTP 方法发送一个 get 请求,在客户端中我将打印包含一些 JSON 数据的响应。
// method from an angular service that sends ajax requests
this.getTask = function(taskName) {
var url = 'http://localhost:9998/tasks/' + taskName;
var config = {
headers: {
'Content-Type': 'application/json'
}
};
$http.get(url, config)
.then(function successCallback(response) {
console.log(response.data);
}, function errorCallback(response) {
console.log(response);
});
};
请求执行成功,但是当response.data语句没有返回JSON数据而是返回这个时;
Object { data: Object, status: 302, headers: headersGetter/<(), config: Object, statusText: "Found" }
通常该语句会打印出上面对象中包含的数据对象。出了什么问题?
【问题讨论】:
-
我的猜测是它没有使用successCallback,而是打印响应的errorCallback。尝试调整您的代码,使其看起来像
then(function(response) {console.log(response.data}).catch(function(err) {console.error(err)});