【问题标题】:When I use "response.data" to print the JSON data, it only prints out whole response instead of the JSON当我使用“response.data”打印 JSON 数据时,它只打印出整个响应而不是 JSON
【发布时间】: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)});

标签: java angularjs json


【解决方案1】:
var data = JSON.parse(apiResponse);

var name = data.response;

【讨论】:

    猜你喜欢
    • 2016-05-30
    • 1970-01-01
    • 2020-01-20
    • 2017-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多