2016-12-05 31 views
-2

我正在從angularjs 1.5x客戶端向Java HTTP方法發送獲取請求,並在客戶端I將打印包含一些JSON數據的響應。當我使用「response.data」來打印JSON數據時,它只打印出整個響應而不是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" }

通常這種說法會打印出包含在上述對象中的數據對象。出了什麼問題?

+1

我的猜測是它沒有使用successCallback,而是使用errorCallback打印出響應。嘗試調整你的代碼看起來像'then(function(response){console.log(response.data))。catch(function(err){console.error(err)});' – thibmaek

回答

-2
var data = JSON.parse(apiResponse); 

var name = data.response;