【问题标题】:json-server - Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extensionjson-server - 跨源请求仅支持协议方案:http、data、chrome、chrome-extension
【发布时间】:2018-04-16 03:46:32
【问题描述】:

我正在运行一个快速服务器以在端口 8000 上为我的 angularJS 应用程序提供服务。我在我的一项服务中使用 $http.get 来访问 API。
我正在使用json-server 在 3000 上托管 API。当我在浏览器中输入 URL 时,我得到了 JSON 服务。但是当我打开应用程序时,在 devtools 中显示以下错误:

XMLHttpRequest 无法加载 localhost:3000/techData。跨源请求仅支持协议方案:http、data、chrome、chrome-extension、https。

这是 angularjs 服务:

techApp.factory('techData', function ($http) {
  let data;
  $http.get('localhost:3000/techData')
    .then((response) => {
      data = response.data;
    });

    return {
      techData: data
    };
});

我看到this issue,了解到json-server允许跨源请求。

【问题讨论】:

    标签: angularjs json-server


    【解决方案1】:

    好的,如果有人在这里,我没有使用带有http 的 URL。更改给$http.get 的参数如下解决了问题:

    $http.get('http://localhost:3000/techData')
    

    【讨论】:

      猜你喜欢
      • 2017-10-11
      • 2016-07-27
      • 2015-12-11
      • 2017-12-25
      • 2020-07-02
      • 1970-01-01
      • 1970-01-01
      • 2018-06-29
      相关资源
      最近更新 更多