简体   繁体   English

JSON数据未显示在angularjs ui-grid中

[英]JSON data not being displayed in angularjs ui-grid

I'm new to AngularJS and so far haven't had any problems until this one... 我是AngularJS的新手,到目前为止还没有遇到任何问题......

I am trying to display json data returned for my REST service call without any luck. 我试图显示为我的REST服务调用返回的json数据,没有任何运气。 I can hard-code in a data array into my controller script file and that will be displayed on my web page just fine however when trying to display my json data I'm not having any luck. 我可以将数据阵列硬编码到我的控制器脚本文件中,这将显示在我的网页上,但是当我试图显示我的json数据时,我没有任何运气。

This is what I currently have coded... 这是我目前编码的......

Web page- 网页-

<div ng-controller="ExceptionLogDataController">
    <div ui-grid="gridOptions" class="vertexGrid"></div>
</div>

ExceptionLogDataController- ExceptionLogDataController-

    $scope.vertexData = [];

    $scope.gridOptions = {
        enableSorting: true,
        data: "vertexData",
        columnDefs: [
          { name: 'Data Id', field: 'DataId' },
          { name: 'Source Date Time', field: 'SourceDateTime' },
          { name: 'Message Text', field: 'MessageText' },
          { name: 'IsDirty', field: 'IsDirty' }
        //  { name: 'FileName', field: 'FileName' },
        //  { name: 'GenJIRATicket', field: 'GenJIRATicket' },
        //  { name: 'MessageCount', field: 'MessageCount' },
        //  { name: 'MachineName', field: 'MachineName' },
        //  { name: 'AppDomainName', field: 'AppDomainName' },
        //  { name: 'ProcessName', field: 'ProcessName' },
        //  { name: 'StackTrace', field: 'StackTrace' }
        ],
    };

    //$scope.vertexData = [
    //    {
    //        "First Name": "John",
    //        "Last Name": "Smith",
    //    },
    //    {
    //        "First Name": "Jane",
    //        "Last Name": "Doe",
    //    }
    //];

    $scope.load = function () {
        ExceptionLogDataFactory()
            .then(function (response) {
                $scope.vertexData = JSON.parse(response.data);
        });
    }

    $scope.load();
}

ExceptionLogDataController.$inject = ['$scope', 'ExceptionLogDataFactory'];

ExceptionLogDataFactory- ExceptionLogDataFactory-

var ExceptionLogDataFactory = function ($http, $q, SessionService) {
    return function () {
        var result = $q.defer();

        $http({
            method: 'GET',
            url: SessionService.apiUrl + '/api/ExceptionLogData',
            headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + SessionService.getToken() }
        })
        .success(function (response) {
            result.resolve(response);
        })
        .error(function (response) {
            result.reject(response);
        });
        return result.promise;
    }
}

ExceptionLogDataFactory.$inject = ['$http', '$q', 'SessionService'];

I've verified that my REST call is returning JSON data through Postman so the problem lies with my front end code. 我已经验证我的REST调用是通过Postman返回JSON数据所以问题在于我的前端代码。

Making progress... 取得进展...

I'm getting my json object successfully returned and am trying to display it with the following... 我正在成功返回我的json对象,并尝试使用以下内容显示它...

$scope.data = [];
$scope.gridOptions = {
    enableSorting: true,
    data: 'data',
};

ExceptionLogDataService() //Call to Service that returns json object
.then(function (data) {
    $scope.data = data;
    $scope.gridOptions.data = $scope.data;
    console.log($scope.data);
}

And this is the json object that is being returned via console.log call... 这是通过console.log调用返回的json对象...

Object { DataId: 1074, SourceDateTime: "2016-01-19T13:29:01.2512456-05:00", MessageText: "There is an error in XML document (…", IsDirty: false, StatusList: Object, FileName: "D:\\ProdMonitorSiteDev\\ErrorFiles\\…", GenJIRATicket: false, MessageCount: 1, MachineName: "VERTEXCUTIL01", AppDomainName: "", 2 more… } 对象{DataId:1074,SourceDateTime:“2016-01-19T13:29:01.2512456-05:00”,MessageText:“XML文档中存在错误(...”,IsDirty:false,StatusList:Object,FileName:“D :\\ ProdMonitorSiteDev \\ ErrorFiles \\ ...“,GenJIRATicket:false,MessageCount:1,MachineName:”VERTEXCUTIL01“,AppDomainName:”“,还有2个......}

This is the error that I am getting... 这是我得到的错误......

Error: newRawData.forEach is not a function 错误:newRawData.forEach不是函数

Well I figured it out! 好吧我明白了!

I finally got my head out of the weeds and 'really' looked at the JSON object that was being returned from my service and noticed that the object was encapsulated with '{}' (curly braces) which is what was causing the newRawData.forEach error. 我终于摆脱了杂草,'真的'看了从我的服务返回的JSON对象,发现该对象是用'{}'(花括号)封装的,这是造成newRawData.forEach的原因错误。 So what I did was the following... 所以我做的是以下......

.then(function (data) {
    $scope.data = "[" + JSON.stringify(data) + "]"; // 'Stringify my object and then encapsulate it with square brackets '[]' and then I could use JSON.parse to then parse the new string into a JSON object...
    $scope.data = JSON.parse($scope.data);

    // Worked like a champ!....
    $scope.gridOptions.data = JSON.stringify($scope.data);

You don't need to parse the JSON. 您不需要解析JSON。

$http.get(url)
.success(function (data) {  
    $scope.gridOptions.data = data;
}

This should work just fine for what you are doing. 这应该对你正在做的事情很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM