【问题标题】:Error when I use TranslateHttpLoader in Angular application在 Angular 应用程序中使用 TranslateHttpLoader 时出错
【发布时间】:2017-08-08 03:56:55
【问题描述】:

我正在使用 angular4 开发应用程序。 现在我正在开发l

export function createTranslateLoader(http: Http) {
    let fullLocationPath = location.host + location.pathname;
    return new TranslateHttpLoader(http, fullLocationPath + 'assets/languages/', '.json');
}

但我得到了下一个错误:

XMLHttpRequest 无法加载 localhost:4200/assets/languages/en.json。 跨域请求仅支持协议方案:http、 数据,铬,铬扩展,https。

我该如何解决它?

【问题讨论】:

  • 我猜你是在文件系统中本地“开发”。您在浏览器中的应用程序的网址是什么?
  • URL - localhost:4200
  • 在运行应用程序或打开构建文件时?

标签: javascript angular http typescript


【解决方案1】:

在代码中看不到你使用fullLocationPath的地方,但是你需要在它前面加上location.protocol,或者更好,使用location.origin

let fullLocationPath = location.origin + location.pathname;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-22
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 2018-02-11
    • 2019-04-30
    • 2020-11-04
    相关资源
    最近更新 更多