AngularJS - 通过$ http get加载时,“数据必须是有效的JSON对象”错误

时间:2017-09-27 09:30:27

标签: javascript angularjs json http

我正在尝试使用我的角应用中的$ http.get请求从json文件中提取员工列表的数据,但控制台显示以下错误:

Error: [$http:baddata] Data must be a valid JSON object. Received: "{......

不确定是什么问题,但我之前没有使用http请求来获取json文件,我认为我的json看起来很好。

-

JSON:

{
'employees': [
    {
        id: '21101994',
        icon: 'img/1.png',
        iconAlt: 'N C Image',
        title: 'Mr',
        firstName: 'N',
        lastName: 'C',
        dateOfBirth: '21/10/1994',
        niNumber: 'JZ123456D',
        jobTitle: 'Web Developer',
        department: 'Development',
        joinDate: '18/04/2017',
        leaveDate: '18/04/2017',
        email: 'example@mail.com',
        phonePrimary: '07123 456789',
        phoneSecondary: '07123 456789',
        menu: 'ncMenu',
        employeePayments: [
            {
                id: 1,
                code: 'BASIC',
                paymentType: 'Basic Pay',
                hours: '123',
                zeroiseHours: 'No',
                partPay: 'No',
                rate: '123.12',
                employerPercentage: '99.99',
                pay: '123.12',
                paymentFrequency: 'Weekly',
                startDate: '26/09/2017',
                sequence: '50',
                employerPension: '',
                csaReference: '',
                suspend: 'No',
                netToGross: 'No',
                requiredTotal: '',
                paidToDate: '',
                protectedNet: '',
                arrearsCarried: ''
            },
            {
                id: 2,
                code: 'CMA',
                paymentType: 'Current Maintenance Assessment',
                hours: '99',
                zeroiseHours: 'No',
                partPay: 'No',
                rate: '99.99',
                employerPercentage: '99.99',
                pay: '99.99',
                paymentFrequency: 'Monthly',
                startDate: '21/10/1994',
                sequence: '4100',
                employerPension: '99.99',
                csaReference: '999',
                suspend: 'Yes',
                netToGross: 'No',
                requiredTotal: '99.99',
                paidToDate: '99.99',
                protectedNet: '99.99',
                arrearsCarried: '99.99'
            },
        ]
    },
    {
        id: '1',
        icon: '',
        iconAlt: 'Employee 2 Image',
        title: 'Mr',
        firstName: 'A',
        lastName: 'Z',
        dateOfBirth: '01/01/2000',
        niNumber: 'JZ123456D',
        jobTitle: 'Job Title',
        department: 'Testing',
        joinDate: '01/01/2017',
        leaveDate: '01/01/2017',
        email: 'example@mail.com',
        phonePrimary: '07000 123456',
        phoneSecondary: '0151 123456',
        menu: 'employee2Menu'
    },
    {
        id: '',
        icon: '',
        iconAlt: 'Employee 3 Image',
        title: 'Mr',
        firstName: 'B',
        lastName: 'Y',
        dateOfBirth: '01/01/2000',
        niNumber: 'JZ123456D',
        jobTitle: 'Job Title',
        department: 'Marketing',
        joinDate: '01/01/2017',
        leaveDate: '01/01/2017',
        email: 'example@mail.com',
        phonePrimary: '07000 123456',
        phoneSecondary: '0151 123456',
        menu: 'employee3Menu'
    },
    {
        id: '',
        icon: '',
        iconAlt: 'Employee 4 Image',
        title: 'Mr',
        firstName: 'C',
        lastName: 'X',
        dateOfBirth: '01/01/2000',
        niNumber: 'JZ123456D',
        jobTitle: 'Job Title',
        department: 'Payroll',
        joinDate: '01/01/2017',
        leaveDate: '01/01/2017',
        email: 'example@mail.com',
        phonePrimary: '07000 123456',
        phoneSecondary: '0151 123456',
        menu: 'employee4Menu'
    },
    {
        id: '',
        icon: '',
        iconAlt: 'Employee 5 Image',
        title: 'Mr',
        firstName: 'Full',
        lastName: 'Name',
        dateOfBirth: '01/01/2000',
        niNumber: 'JZ123456D',
        jobTitle: 'Job Title',
        department: 'Marketing',
        joinDate: '01/01/2017',
        leaveDate: '01/01/2017',
        email: 'example@mail.com',
        phonePrimary: '07000 123456',
        phoneSecondary: '0151 123456',
        menu: 'employee5Menu'
    },
    {
        id: '',
        icon: '',
        iconAlt: 'Employee 6 Image',
        title: 'Mr',
        firstName: 'Full',
        lastName: 'Name',
        dateOfBirth: '01/01/2000',
        niNumber: 'JZ123456D',
        jobTitle: 'Job Title',
        department: 'Support',
        joinDate: '01/01/2017',
        leaveDate: '01/01/2017',
        email: 'example@mail.com',
        phonePrimary: '07000 123456',
        phoneSecondary: '0151 123456',
        menu: 'employee6Menu'
    },
    {
        id: '',
        icon: '',
        iconAlt: 'Employee 7 Image',
        title: 'Mr',
        firstName: 'Full',
        lastName: 'Name',
        dateOfBirth: '01/01/2000',
        niNumber: 'JZ123456D',
        jobTitle: 'Job Title',
        department: 'Project Management',
        joinDate: '01/01/2017',
        leaveDate: '01/01/2017',
        email: 'example@mail.com',
        phonePrimary: '07000 123456',
        phoneSecondary: '0151 123456',
        menu: 'employee7Menu'
    },
    {
        id: '',
        icon: '',
        iconAlt: 'Employee 8 Image',
        title: 'Mr',
        firstName: 'Full',
        lastName: 'Name',
        dateOfBirth: '01/01/2000',
        niNumber: 'JZ123456D',
        jobTitle: 'Job Title',
        department: 'Development',
        joinDate: '01/01/2017',
        leaveDate: '01/01/2017',
        email: 'example@mail.com',
        phonePrimary: '07000 123456',
        phoneSecondary: '0151 123456',
        menu: 'employee8Menu'
    },
    {
        id: '',
        icon: '',
        iconAlt: 'Employee 9 Image',
        title: 'Mr',
        firstName: 'X',
        lastName: 'C',
        dateOfBirth: '01/01/2000',
        niNumber: 'JZ123456D',
        jobTitle: 'Job Title',
        department: 'Project Management',
        joinDate: '01/01/2017',
        leaveDate: '01/01/2017',
        email: 'example@mail.com',
        phonePrimary: '07000 123456',
        phoneSecondary: '0151 123456',
        menu: 'employee9Menu'
    },
    {
        id: '',
        icon: '',
        iconAlt: 'Employee 10 Image',
        title: 'Mr',
        firstName: 'Y',
        lastName: 'B',
        dateOfBirth: '01/01/2000',
        niNumber: 'JZ123456D',
        jobTitle: 'Job Title',
        department: 'Management',
        joinDate: '01/01/2017',
        leaveDate: '01/01/2017',
        email: 'example@mail.com',
        phonePrimary: '07000 123456',
        phoneSecondary: '0151 123456',
        menu: 'employee10Menu'
    },
    {
        id: '12673855',
        icon: '',
        iconAlt: 'Employee 11 Image',
        title: 'Dr',
        firstName: 'Z',
        lastName: 'A',
        dateOfBirth: '01/01/2000',
        niNumber: 'JZ123456D',
        jobTitle: 'Managing Director',
        department: 'Management',
        joinDate: '01/01/2017',
        leaveDate: '01/01/2017',
        email: 'example@mail.com',
        phonePrimary: '07000 123456',
        phoneSecondary: '0151 123456',
        menu: 'employee11Menu'
    }
]
}

-

控制器:

app.controller('employeesListController', function($scope, $http, $state, $stateParams, employeesService) {
$scope.active = 'active';

$scope.sortEmployeesBy = '+lastName';

$scope.titles = employeesService.getTitles();
$scope.departments = employeesService.getDepartments();

$http.get('js/data/employees.json')
.then(function(response) {
    angular.fromJson(response);
    $scope.employees = response.data.employees;
})

2 个答案:

答案 0 :(得分:2)

您可以使用https://jsonlint.com/来验证json。正如ADreNaLiNe-DJ评论说由于缺少引号而导致问题

答案 1 :(得分:0)

或者您可以使用JSON.stringify



const emp = {
'employees': [
    {
        id: '21101994',
        icon: 'img/1.png',
        iconAlt: 'N C Image',
        title: 'Mr',
        firstName: 'N',
        lastName: 'C',
        dateOfBirth: '21/10/1994',
        niNumber: 'JZ123456D',
        jobTitle: 'Web Developer',
        department: 'Development',
        joinDate: '18/04/2017',
        leaveDate: '18/04/2017',
        email: 'example@mail.com',
        phonePrimary: '07123 456789',
        phoneSecondary: '07123 456789',
        menu: 'ncMenu',
        employeePayments: [
            {
                id: 1,
                code: 'BASIC',
                paymentType: 'Basic Pay',
                hours: '123',
                zeroiseHours: 'No',
                partPay: 'No',
                rate: '123.12',
                employerPercentage: '99.99',
                pay: '123.12',
                paymentFrequency: 'Weekly',
                startDate: '26/09/2017',
                sequence: '50',
                employerPension: '',
                csaReference: '',
                suspend: 'No',
                netToGross: 'No',
                requiredTotal: '',
                paidToDate: '',
                protectedNet: '',
                arrearsCarried: ''
            },
            {
                id: 2,
                code: 'CMA',
                paymentType: 'Current Maintenance Assessment',
                hours: '99',
                zeroiseHours: 'No',
                partPay: 'No',
                rate: '99.99',
                employerPercentage: '99.99',
                pay: '99.99',
                paymentFrequency: 'Monthly',
                startDate: '21/10/1994',
                sequence: '4100',
                employerPension: '99.99',
                csaReference: '999',
                suspend: 'Yes',
                netToGross: 'No',
                requiredTotal: '99.99',
                paidToDate: '99.99',
                protectedNet: '99.99',
                arrearsCarried: '99.99'
            },
        ]
    },
    {
        id: '1',
        icon: '',
        iconAlt: 'Employee 2 Image',
        title: 'Mr',
        firstName: 'A',
        lastName: 'Z',
        dateOfBirth: '01/01/2000',
        niNumber: 'JZ123456D',
        jobTitle: 'Job Title',
        department: 'Testing',
        joinDate: '01/01/2017',
        leaveDate: '01/01/2017',
        email: 'example@mail.com',
        phonePrimary: '07000 123456',
        phoneSecondary: '0151 123456',
        menu: 'employee2Menu'
    },
    {
        id: '',
        icon: '',
        iconAlt: 'Employee 3 Image',
        title: 'Mr',
        firstName: 'B',
        lastName: 'Y',
        dateOfBirth: '01/01/2000',
        niNumber: 'JZ123456D',
        jobTitle: 'Job Title',
        department: 'Marketing',
        joinDate: '01/01/2017',
        leaveDate: '01/01/2017',
        email: 'example@mail.com',
        phonePrimary: '07000 123456',
        phoneSecondary: '0151 123456',
        menu: 'employee3Menu'
    },
    {
        id: '',
        icon: '',
        iconAlt: 'Employee 4 Image',
        title: 'Mr',
        firstName: 'C',
        lastName: 'X',
        dateOfBirth: '01/01/2000',
        niNumber: 'JZ123456D',
        jobTitle: 'Job Title',
        department: 'Payroll',
        joinDate: '01/01/2017',
        leaveDate: '01/01/2017',
        email: 'example@mail.com',
        phonePrimary: '07000 123456',
        phoneSecondary: '0151 123456',
        menu: 'employee4Menu'
    },
    {
        id: '',
        icon: '',
        iconAlt: 'Employee 5 Image',
        title: 'Mr',
        firstName: 'Full',
        lastName: 'Name',
        dateOfBirth: '01/01/2000',
        niNumber: 'JZ123456D',
        jobTitle: 'Job Title',
        department: 'Marketing',
        joinDate: '01/01/2017',
        leaveDate: '01/01/2017',
        email: 'example@mail.com',
        phonePrimary: '07000 123456',
        phoneSecondary: '0151 123456',
        menu: 'employee5Menu'
    },
    {
        id: '',
        icon: '',
        iconAlt: 'Employee 6 Image',
        title: 'Mr',
        firstName: 'Full',
        lastName: 'Name',
        dateOfBirth: '01/01/2000',
        niNumber: 'JZ123456D',
        jobTitle: 'Job Title',
        department: 'Support',
        joinDate: '01/01/2017',
        leaveDate: '01/01/2017',
        email: 'example@mail.com',
        phonePrimary: '07000 123456',
        phoneSecondary: '0151 123456',
        menu: 'employee6Menu'
    },
    {
        id: '',
        icon: '',
        iconAlt: 'Employee 7 Image',
        title: 'Mr',
        firstName: 'Full',
        lastName: 'Name',
        dateOfBirth: '01/01/2000',
        niNumber: 'JZ123456D',
        jobTitle: 'Job Title',
        department: 'Project Management',
        joinDate: '01/01/2017',
        leaveDate: '01/01/2017',
        email: 'example@mail.com',
        phonePrimary: '07000 123456',
        phoneSecondary: '0151 123456',
        menu: 'employee7Menu'
    },
    {
        id: '',
        icon: '',
        iconAlt: 'Employee 8 Image',
        title: 'Mr',
        firstName: 'Full',
        lastName: 'Name',
        dateOfBirth: '01/01/2000',
        niNumber: 'JZ123456D',
        jobTitle: 'Job Title',
        department: 'Development',
        joinDate: '01/01/2017',
        leaveDate: '01/01/2017',
        email: 'example@mail.com',
        phonePrimary: '07000 123456',
        phoneSecondary: '0151 123456',
        menu: 'employee8Menu'
    },
    {
        id: '',
        icon: '',
        iconAlt: 'Employee 9 Image',
        title: 'Mr',
        firstName: 'X',
        lastName: 'C',
        dateOfBirth: '01/01/2000',
        niNumber: 'JZ123456D',
        jobTitle: 'Job Title',
        department: 'Project Management',
        joinDate: '01/01/2017',
        leaveDate: '01/01/2017',
        email: 'example@mail.com',
        phonePrimary: '07000 123456',
        phoneSecondary: '0151 123456',
        menu: 'employee9Menu'
    },
    {
        id: '',
        icon: '',
        iconAlt: 'Employee 10 Image',
        title: 'Mr',
        firstName: 'Y',
        lastName: 'B',
        dateOfBirth: '01/01/2000',
        niNumber: 'JZ123456D',
        jobTitle: 'Job Title',
        department: 'Management',
        joinDate: '01/01/2017',
        leaveDate: '01/01/2017',
        email: 'example@mail.com',
        phonePrimary: '07000 123456',
        phoneSecondary: '0151 123456',
        menu: 'employee10Menu'
    },
    {
        id: '12673855',
        icon: '',
        iconAlt: 'Employee 11 Image',
        title: 'Dr',
        firstName: 'Z',
        lastName: 'A',
        dateOfBirth: '01/01/2000',
        niNumber: 'JZ123456D',
        jobTitle: 'Managing Director',
        department: 'Management',
        joinDate: '01/01/2017',
        leaveDate: '01/01/2017',
        email: 'example@mail.com',
        phonePrimary: '07000 123456',
        phoneSecondary: '0151 123456',
        menu: 'employee11Menu'
    }
]
};

console.log(JSON.stringify(emp, null, 2));