简体   繁体   English

使用AngularJS循环遍历JSON文件时出现“格式不正确”的firefox错误

[英]“Not well-formed” firefox error while looping through JSON file with AngularJS

The JSON file structure is as follows: JSON文件结构如下:

{
    "meta": {
        "limit": 20, 
        "next": null, 
        "offset": 0, 
        "previous": null, 
        "total_count": 2
    }, 
    "objects": [
        {
            "description": "Lorem ipsum dolor sit amet ....Lorem ipsum dolor sit amet ....Lorem ipsum dolor sit amet ....", 
            "end": "2013-09-09T15:37:24", 
            "id": 1, 
            "name": "Lorem ipsum dolor sit amet ....Lorem ipsum dolor sit amet", 
            "resource_uri": "/api/task/1/", 
            "start": "2013-09-09T15:37:23"
        },
        {
            "description": "Here goes description :) Here goes description :) Here goes description :) ", 
            "end": "2013-09-11T15:58:01", 
            "id": 2, 
            "name": "Some other task", 
            "resource_uri": "/api/task/2/", 
            "start": "2013-09-11T15:58:00"
        }
    ]
}

I was trying to loop through it like so: 我试图像这样循环:

<!DOCTYPE html>
<html lang="en" ng-app id="ng-app">
    <head>
        <title>Angular studies</title>
        <meta charset="UTF-8" />
        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
        <script>
        function PostsCtrlAjax($scope, $http) {
        $http({method: 'POST', url: 'posts.json'}).success(function(data) {
                $scope.tasks = data;
            });
        }
        </script>
    </head>
    <body>
        <div id="ng-app" ng-app ng-controller="PostsCtrlAjax"> 

        <div ng-repeat="task in tasks">
           <a href='{{task.id}}'>{{task.name}}</a>
        </div>

        </div>
    </body>
</html>

And for this I'm getting "Not well-formed" error in Firefox. 为此,我在Firefox中遇到“格式不正确”的错误。 Can you please show me a way to mend this? 你能告诉我一个修补这个的方法吗?

This is indeed valid JSON. 这确实是有效的JSON。 The problem is that you try to iterate over tasks , but in fact want to iterate over tasks.objects 问题是你尝试迭代tasks ,但实际上想要迭代tasks.objects

for (var i=0;i<tasks.objects.length;i++) {
    console.log(tasks.objects[i].id);
    console.log(tasks.objects[i].name);
}

outputs 输出

1 
Lorem ipsum dolor sit amet ....Lorem ipsum dolor sit amet 
2 
Some other task 

I am not familiar with the angular syntax, but I guess this is what you want / really try to do : 我不熟悉角度语法,但我想这是你想要/真正尝试做的:

<div ng-repeat="task in tasks.objects">

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

相关问题 使用 XMLHttpRequest 加载 JSON 文件时,Firefox 中出现“格式不正确”错误 - "not well-formed" error in Firefox when loading JSON file with XMLHttpRequest AngularJS“格式不正确”,但仍在Firefox中显示 - AngularJS “not well-formed”, but still showing in firefox jQuery AJAX从文件中检索SVG-错误:格式不正确 - jQuery AJAX retrieve SVG from file - ERROR: 'not well-formed' javascript的“解析XML时出错:格式不正确” - "Error parsing XML: not well-formed" for javascript jquery加载函数在firefox中抛出“XML解析错误:格式不正确”错误 - Jquery load function throws “XML Parsing Error: not well-formed” error in firefox JavaScript例外:firefox浏览器上的“格式不正确”,Chrome浏览器上的“ XMLHttpRequest ..”错误 - JavaScript exception: “not well-formed” on firefox browser and “XMLHttpRequest..” error on chrome browser 使用jQuery访问JSON时格式错误 - Not well formed error while accessing JSON with jquery javascript格式不正确 - javascript not well-formed XML解析错误:使用浏览器的内置解析器解析XML字符串时,格式不正确 - XML Parsing Error: not well-formed while parsing XML string with browser's built in parser 格式良好的 JSON 文件 - Well Formed JSON file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM