Anglarjs2 - Http get request:错误:无法解析url ..原始错误:无法在[null]中读取未定义的属性'split'

siv*_*iva 4 http-get angular

当我在Hero.service.ts中使用get url作为web api时,在浏览器控制台中出现以下错误.但是当我在一个项目中使用.Json文件时它工作正常,因为我能够在输出页面中看到值.我刚刚在angular.io网站上学习.

以下是控制台中的错误:

angular2-polyfills.js:471 Error: Uncaught (in promise): EXCEPTION: Error: unable to parse url 'http://md5.jsontest.com/?text=example_text'; original error: Cannot read property 'split' of undefined in [null](…)consoleError @ angular2-polyfills.js:471

Get url: http://md5.jsontest.com/?text=example_text
Run Code Online (Sandbox Code Playgroud)

这是完整的代码,

Plunker

你能帮我解决一下我在做错的事吗?

Thi*_*ier 6

您的问题来自于InMemoryBackendService需要以下URL格式(路径的两个级别):

http://host:port/base/collectionName
Run Code Online (Sandbox Code Playgroud)

在您的情况下,服务抛出异常,因为`collectionName未定义...

要使其工作,要么使用遵循此格式的URL,要么InMemoryBackendService从提供程序中删除该类:

providers: [
  HTTP_PROVIDERS,
  HeroService,
  // in-memory web api providers
  provide(XHRBackend, { useClass: InMemoryBackendService }), // <---
Run Code Online (Sandbox Code Playgroud)