Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.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 在Angular中用本地json替换外部JSONP_Javascript_Json_Angularjs - Fatal编程技术网

Javascript 在Angular中用本地json替换外部JSONP

Javascript 在Angular中用本地json替换外部JSONP,javascript,json,angularjs,Javascript,Json,Angularjs,我将在与此Angular应用程序相同的服务器上存储一个json文件,我想知道如何更改此代码以读取本地存储的json文件: ergastAPI.getDrivers = function() { return $http({ method: 'JSONP', url: 'http://somedomain.com/datafile.json?callback=JSON_CALLBACK' }); } 我的Json文件将存储在http://mysite.com/app/j

我将在与此Angular应用程序相同的服务器上存储一个json文件,我想知道如何更改此代码以读取本地存储的json文件:

ergastAPI.getDrivers = function() {
  return $http({
    method: 'JSONP', 
    url: 'http://somedomain.com/datafile.json?callback=JSON_CALLBACK'
  });
}
我的Json文件将存储在http://mysite.com/app/js/datafile.Json,而应用程序的其余部分将存储在其父文件夹app中


我必须将方法更改为jsonp以外的其他方法吗?

您需要更改方法以获取,我认为您必须将responseType更改为“JSON”或类似的内容。(我现在正在打电话,请原谅我的简短)

如果是同一个域,您只需使用$http.get即可

ergastAPI.getDrivers = function() {
  return $http.get('app/js/datafile.json');
}