2016-10-28 153 views
0

我想從指定的URL返回JSON數據,但當警報彈出它只是顯示[對象對象]。我打算日期顯示在日曆中,但僅限於您進入json的日期,而不是所有日子。我該怎麼做?JSON返回[對象對象] angularjs

我的日曆

enter image description here

這裏是JSON的一個例子。

{ 
    "title": "example glossary", 
    "start": "2016-10-22", 
    "allDay": false 
} 

控制器

$scope.setDayContent = function(date) { 

    return $http  ({ 
     method : "GET", 
     url : "app/components/home/controller/test_calendar.json" 
     }).then(function mySucces(response) { 
      return response.data;    
     }, function myError(response) { 
      $scope.valor = response.statusText; 
     }); 

}; 

如果我把它這樣,它工作正常<p>A data {{valor.start}}</p>,但我要的是出現在日曆上,然後在錯誤出現[對象的對象。

+0

打印使用'的console.log(響應)錯誤;'看的對象。警報無法將對象文字解析爲字符串。 –

+0

警報在哪裏? –

+0

如果我把console.log(response.data)他給我的所有參數在我的JSON對象,但我只有一個對象,並循環。返回20個對象是否正常?我只有1 –

回答

0

嘗試的onSuccess函數來設置值:

$scope.setDayContent = function(date) { 
    return $http  ({ 
     method : "GET", 
     url : "app/components/home/controller/test_calendar.json" 
    }).then(function(response) { 
     $scope.valor = response.data; 
    }); 
}; 
1

var data = { 
 
     "title": "example glossary", 
 
     "start": "2016-10-22", 
 
     "allDay": false 
 
    } 
 
var json = JSON.stringify(data); 
 
alert(json)

{ 
    "title": "example glossary", 
    "start": "2016-10-22", 
    "allDay": false 
} 

這不是JSON,這是JS對象文本。

使它成爲json。

var data = { 
     "title": "example glossary", 
     "start": "2016-10-22", 
     "allDay": false 
    } 
var json = JSON.stringify(data); 
0

我不知道你這是什麼意思:「我打算將日期顯示在日曆中,但僅在一天,你進入JSON,而不是所有的日子。」

你在哪裏進入JSON(這可能不是你的意思,但這就是它所說的)。

然後你說「如果我這樣做,它的工作原理......」但似乎工作的是,你的電話會導致錯誤。

另外,「valor」出現在您的錯誤消息中。我假設你不想在日曆中顯示錯誤信息。

如果我不得不猜測,我會說你可能將Succes中的整個返回對象(希望這裏只是你的示例中的一個拼寫錯誤)綁定到你的模板中,就像{{theObj}},而不是你想要的具體價值,比如「{{theObj.date}}」等等。

(BTW我精明白你所說的「這是我的JSON」的意思。)