4

Getting below error in browser console, when I'm using get Url as web api in Hero.service.ts. But It's working fine when I used with .Json file with in a that project as I'm able to see the values in output page. I'm just learning with angular.io website.

Below is the error in the console:

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

Here is that entire code available,

Plunker

Could you please help me what i'm doing wrong in this?

Ankit Singh
  • 24,525
  • 11
  • 66
  • 89
siva
  • 197
  • 4
  • 10

1 Answers1

6

You problem comes from the InMemoryBackendService which expects the following URL format (two levels for path):

http://host:port/base/collectionName

In your case, the service throws an exception because `collectionName is undefined...

To make it work either you use URLs that follow this format either you remove the InMemoryBackendService class from the providers:

providers: [
  HTTP_PROVIDERS,
  HeroService,
  // in-memory web api providers
  provide(XHRBackend, { useClass: InMemoryBackendService }), // <---
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • Thanks Thierry for your help. Also I have one more question: If the web api returns XML's instead of Json means how this will work. Whether we have any specific syntax like for Json-- > data.json(). – siva Apr 15 '16 at 17:01
  • 1
    You're welcome! XML isn't supported out of the box by Angular2. See this question on how to use this format with the framework: http://stackoverflow.com/questions/36368405/how-to-parse-xml-in-angular-2/36370061#36370061. – Thierry Templier Apr 16 '16 at 07:01