javascript - 如何使用 $http.get 加载内部 json 而不是外部 JSON 文件

标签 javascript angularjs

我使用一个从外部 json 文件加载字符串的代码片段。一切工作正常,但是当我在本地启动该功能时,我遇到了“跨源”问题。因此,我想将字符串直接放入我的JS中,但它不起作用。

Origin JS

$scope.loadAutosuggest = function(query) {
    return $http.get('data.json');
};

data.json

[
    "In Progress: Yes",
    "In Progress: No"
]

我尝试过像这里一样执行此操作(但不起作用)

$scope.loadAutosuggest = [
 "In Progress: Yes",
 "In Progress: No"          
];

你有什么建议给我吗?谢谢

最佳答案

问题可能是 $scope.loadAutosuggest 应该是一个函数。

$scope.loadAutosuggest = function() {
  return [
    "In Progress: Yes",
    "In Progress: No"          
  ];
};

关于javascript - 如何使用 $http.get 加载内部 json 而不是外部 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30964580/

相关文章:

javascript - 使用 CakePHP 进行实时应用

javascript - 结构与 Javascript 代码? CSS、Divs,然后是脚本?

javascript - 我如何修改脚本以打印 mysql 查询中的所有元素 - php mysql javascript ajax

javascript - 使用带有新 id 的 javascript 克隆 html 中的 div

html - 在网站的移动 View 中点击链接时遇到问题?

javascript - Angular 处理不等待 http.get .then

javascript - Angular.js UNIT 测试指令

javascript - `import type {Node} from ' 有什么 react ';` 以及在 App : () => Node 中的用法

javascript - 使用 AngularJs 将 html 内容作为纯文本粘贴到 contenteditable div 中

javascript - 使用 Ionic/AngularJS 和 Cordova 为 native 和 Web 应用程序使用相机