Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从服务器加载外部jsonfile时出现Angularjs错误_Javascript_Json_Angularjs_Ionic Framework - Fatal编程技术网

Javascript 从服务器加载外部jsonfile时出现Angularjs错误

Javascript 从服务器加载外部jsonfile时出现Angularjs错误,javascript,json,angularjs,ionic-framework,Javascript,Json,Angularjs,Ionic Framework,我正在尝试从服务器加载json文件。这是我的services.js文件 angular.module('starter.services', []) /** * A simple example service that returns some data. */ .factory('Friends', function($http) { var friends; //here is the code for server access $http.get('http://ww

我正在尝试从服务器加载json文件。这是我的services.js文件

angular.module('starter.services', [])

/**
 * A simple example service that returns some data.
 */
.factory('Friends', function($http) {

  var friends;
 //here is the code for server access
  $http.get('http://wwww.root5solutions.com/ionictest/get.json').then(function(msg){
      console.log("console output"+msg.data);
      friends=msg.data;
      console.log("from server"+friends.data);
  });

  return {
    all: function() {
      return friends;
    },
    get: function(friendId) {

      return friends[friendId];
    }
  }
});
但它没有正常工作。这是我的安慰信息

XMLHttpRequest cannot load http://www.root5solutions.com/ionictest/get.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access. 

您必须将该URL列入白名单

试试这个

.config(function($sceDelegateProvider) {

    $sceDelegateProvider.resourceUrlWhitelist([
        // Allow same origin resource loads.
        'self',
        // Allow loading from our other assets domain.  Note there is a difference between * and **.
        'http://wwww.root5solutions.com/ionictest/**',
    ]);
})

我也遇到了同样的问题。我相信这会解决你的问题,

对于Windows。。。在桌面上创建Chrome快捷方式>右clic>属性>快捷方式>编辑“目标”路径:>“C:\Program Files\Google\Chrome\Application\Chrome.exe”-args--禁用web安全

注意:设置此选项后,退出chrome,然后打开



所有这些问题都是因为我只使用浏览器进行了测试。在设备/模拟器上测试时,它的工作状态非常好。不需要额外的代码来解决这个问题。

服务器不支持CORS,请参阅如何启用它。你能展示meI的示例代码吗?我刚刚在js文件中添加了以下代码,但是没有工作
$http.defaults.headers.put={'Access Control Allow Origin':'*'}由服务器决定是否提供CORS支持。您无法在javascript中启用它。我在localhost中尝试过,但没有成功。是否可能在localhosthttp中工作正常。但是http post reutrns访问控制允许标头不允许此错误
请求标头字段内容类型参考此内容并检查此内容最后我发现了问题,但从未找到解决方案。Http正常工作,post返回相同的错误。我的答案是否对您有帮助?您能否对此给出答案()