如何加载JSON文件

如何加载JSON文件

本文介绍了如何加载JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi。



我正在创建一个Windows表单项目,用于从数据库中收集数据并将其作为图表显示在html文件中。

收集了我想要的所有数据后,我用这些值更新现有的json文件,以便我可以阅读并使用它们来创建图表。我的问题是,当我尝试从javascript访问json文件时,我收到此错误



hi.

I'm creating a Windows form project to collect data from a database and present it as charts in a html file.
After collecting all the data i want, i update an existing json file with those values so i can read and use them to create the charts. My problem is when i try to access the json file from the javascript i get this error

引用:

xmlhttprequest无法加载文件(文件的路径)交叉原始请求仅支持http

xmlhttprequest cannot load file (path of the file) cross origin requests are only supported for http





我也使用angularjs 。

这就是我的代码:



data.json



I'm also using angularjs.
This is how i have my code:

data.json

{
  "children":[
    {"data": "val1" },
    {"data": "val2" },
    {"data": "val3" },
    {"data": "val4" },
    {"data": "val5" }
  ]
}





Service.js



Service.js

var jsonService = angular.module('jsonService', ['ngResource']);

jsonService.factory('JsonService', function ($resource) {
    return $resource('C:/Users/Lopez/Documents/Visual Studio 2010/Projects/Dashboard/SMO/html/data.json');
});





app.js



app.js

var app = angular.module('angularjs-starter', ['jsonService']);

app.controller('MainCtrl', function ($scope, JsonService) {
    JsonService.get(function (data) {
        $scope.name = data.name;
        $scope.children = data.children;
    });
});





index.htm



index.htm

<!DOCTYPE html>
<html ng-app="angularjs-starter">

  <head lang="en">
    <meta charset="utf-8">
    <title>Custom Plunker</title>

    <script src="../Scripts/angular.js" type="text/javascript"></script>
    <script src="../Scripts/angular-resource.min.js" type="text/javascript"></script>

    <script src="../javascript/service.js" type="text/javascript"></script>
    <script src="../javascript/app.js" type="text/javascript"></script>


  </head>

  <body ng-controller="MainCtrl">

    <h2>children</h2>
    <div ng-repeat="child in children">
      {{child.data}}
    </div>


  </body>

</html>





编辑:

我被告知使用WCF WebService来避免错误消息访问数据。我已经在网上搜索并阅读了教程,但我不明白我怎么能做一个服务,但是消息说不能这样做。



你能给我吗?任何提示?



thk




I've been told to use a WCF WebService to avoid that error message and access the data. I have search the web and read tutorials but i don't understand how can i make a service to do waht the message says it cannot do.

Can you give me any hints?

thk

推荐答案






app.js



app.js

var app = angular.module('angularjs-starter', ['jsonService']);

app.controller('MainCtrl', function (



这篇关于如何加载JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 19:28