【问题标题】:not well-formed Source File in mozilla firefoxMozilla firefox中格式不正确的源文件
【发布时间】:2014-08-08 06:50:52
【问题描述】:

以下是我的JSON文件的内容——

{
    "tags": [
        "Red",
        "Green",
        "Blue",
        "Yellow"
    ]
}

我使用jsonlint 进行了检查,但在 Firefox 中仍然出现以下错误。

时间戳:2014 年 6 月 18 日星期三 10:39:41 IST 错误:格式不正确 源文件: file:///home/trialcode/trialcode/Projects/ang/18-06-2014/ang/content.json 行:1,列:1 源代码:{

我不确定自己做错了什么。

仅供参考-

操作系统 - Linux Ubuntu 12.04

火狐 - 24.0

编辑

我正在通过$http.get 方法在角度控制器中使用content.json 文件的内容。

我对这种错误进行了更多探索,发现它与Content-Type设置有关。

以下是完整代码-

<!doctype html>
<html lang="en" ng-app="app">
<head>
    <title>JSON Read In Angularjs</title>
    <meta charset="UTF-8" />
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>
    <script>
        var app = angular.module('app', []);

        app.controller('myCtrl', function($scope, $http){
            $scope.data = {};
            $http.get('content.json').success(function(data) {
                $scope.data = data;
            });
        });
    </script>
</head>
<body ng-controller="myCtrl">
        <ul>
            <li ng-repeat="em in data.tags">{{em}}</li>
        </ul>
</body>
</html>

如果有问题,我该如何设置内容类型。我搜索了HERE,但无法修复它。有的话请帮帮我。

【问题讨论】:

  • 不确定您的实际问题是什么,但如果您想设置Content-Type HTTP 标头,您可以通过向$http.get 提供配置参数来实现(即$http.get('content.json', {headers: {'Content-Type': 'application/json'}})
  • @miqid 我试过了,但在 FF 中仍然出现“格式不正确”的错误
  • 忽略我之前的评论,我脑子里放了个屁——不管怎样,用Content-Type 发出 GET 是没有意义的。另一个建议是检查返回content.json 的服务器是否将application/json 设置为MIME 类型。或者,如果您不使用服务器,我认为这个特定问题可能会有所帮助:stackoverflow.com/questions/2618959/…
  • @miqid 我遇到了这个stackoverflow.com/a/24281117/1594368

标签: json angularjs firefox content-type http-get


【解决方案1】:

经过几个小时的搜索,我发现了 -

Chrome 和其他现代浏览器已实现安全性 跨域请求的限制,这意味着您不能 通过 file:/// 加载任何内容,您需要使用 http:// 协议 任何时候,甚至是本地到期的同源政策。

来源-

  1. Cross Origin Script Stackoverflow Answer
  2. Simple Solution For Local Cross Origin Requests

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-23
    • 2014-01-13
    • 2011-07-21
    • 2016-04-08
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多