在Angular.js访问JSON文件 [英] Access JSON file in Angular.js

查看:103
本文介绍了在Angular.js访问JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过以下code访问JSON文件中angular.js但我无法访问,因为下面提到的显示错误的code。
帮助将AP preciated !!

模块:

  angular.module(demoApp,['demoApp.factory','demoApp.controllers']);

厂址:

  angular.module('demoApp.factory',[])。
工厂('getJsonDataFactory',函数($ HTTP){
    返回{
    的getData:功能(){
        返回$ http.get('mapData.json');
    }
 }
});

控制器:

  angular.module('demoApp.controllers',[])。
  控制器('GetJsonController',函数($范围,$ HTTP,getJsonDataFactory){
              $ scope.markers = [];
              getJsonDataFactory.getData()。成功(功能(数据){
                $ scope.photos =数据;
            });
        });

错误:
在Firefox中:

 访问受限制的URI被拒绝code:1012
返回logFn.apply(控制台,参数)

在Chrome浏览器:`

 原产地空不被访问控制允许来源允许的。


解决方案

事实上,我认为你没有一台服务器。

所以,你通过让你的应用程序文件:// 协议

文件:// 协议不能做HTTP GET出于安全原因的方法。

要解决你的事情,我建议你把你的应用程序的简单Web服务器(如Apache,或WAMP,XAMP,灯具等),或者最好使用咕噜来构建应用程序,并在测试服务器上通过节点运行它。 JS

I want to access json file in angular.js by using the following code but I am unable to access the code because of the below mentioned shown error. Help would be appreciated.!!

module :

angular.module("demoApp", ['demoApp.factory','demoApp.controllers']);

Factory:

angular.module('demoApp.factory', []).
factory('getJsonDataFactory',function($http){
    return{
    getData : function() {
        return $http.get('mapData.json');
    }
 }
});

controller :

angular.module('demoApp.controllers', []).
  controller('GetJsonController', function($scope,$http,getJsonDataFactory) {
              $scope.markers = [];  
              getJsonDataFactory.getData().success(function(data){
                $scope.photos=data;
            });
        });

Error : In firefox:

 "Access to restricted URI denied" code: "1012"


return logFn.apply(console, args)

In Chrome :`

Origin null is not allowed by Access-Control-Allow-Origin.

解决方案

In fact i think you don't have a server.

So, you're getting your application through the file:// protocol.

But file:// protocol can't do http get methods for security reasons.

To solve your matter i suggest you to put your application on a simple web server (like Apache, or wamp, xamp, lamp etc) or better to use grunt to build your application and run it on a test server through node.js

这篇关于在Angular.js访问JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆