Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

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


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
C#
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
PHP
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
XML
<!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>


EDIT:
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
Posted
Updated 3-Dec-13 12:40pm
v3

1 solution

Hi,

As much I can understand you have the data and then you write in a file and try to access it using Javascript from same machine.

I would suggest you to change your approach.

1 : Fetch Data From DB
2 : Serialize that data using JSON Serialization
3 : Then write that data in your HTML page
eg
HTML
<html>
          <title>
          <script>var Json="{}"; // your data
           </script>
           <title>
<body>
do your operation 
</body>
</title></title></html>


Save the HTML file as template and then replace that block using your data
 
Share this answer
 
Comments
Lopezd 4-Dec-13 4:38am    
Saving the data as string? but then how can i pass it to javascript?
Suvabrata Roy 4-Dec-13 5:07am    
That HTML is a template to you and you will replace certain area with that JSON values as I mentioned in my solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900