我正在尝试从Angular2应用程序发出一个简单的http GET请求:

    this.http.get("https://mybaseurl.com/hello")
  .map(res => res.json())
  .subscribe(
    function(response) { console.log("Success Response" + response)},
    function(error) { console.log("Error happened" + error)},
    function() { console.log("the subscription is completed")}
  );


Node.js服务器的配置方式如下:

app.get('/hello', function(req, res) {
  res.send("hellow world");
});


拨打电话时,我不断收到此错误消息:

    caused by: unable to parse url 'https://mybaseurl.com/hello'; original error: Cannot read property 'split' of undefined
   at InMemoryBackendService.parseUrl (in-memory-backend.service.ts:518)
    at InMemoryBackendService.handleRequest (in-memory-backend.service.ts:279)
    at InMemoryBackendService.createConnection (in-memory-backend.service.ts:240)


知道我在做什么错吗?

编辑:粘贴整个类代码:

    import {Component} from '@angular/core';
import {Auth} from './auth.service';
import {AuthHttp} from 'angular2-jwt';
import {Http} from '@angular/http';
import 'rxjs/add/operator/map';

@Component({
  selector: 'ping',
  templateUrl: 'app/ping.template.html'
})
export class ApiService {

  API_URL: string = 'https://myapp.herokuapp.com';
  message: string;

  constructor(private auth: Auth, private http: Http, private authHttp: AuthHttp) {}

  public ping() {

    this.http.get(`${this.API_URL}/hello`)
      .map(res => res.json())
      .subscribe((response) => {
        console.log("Success Response" + response)
        },
        (error) => { console.log("Error happened" + error)},
        () => { console.log("the subscription is completed")}
    );
  }
}


=====>
这看起来像Angular2中的一个巨大错误-所有http requests返回null,或者一条错误消息,描述它不在所需的模式中。
我想看到一个工作演示HTTP GET

最佳答案

似乎同时在InMemoryWebApiModule.forRoot(InMemoryDataService)@NgModule中使用Http.get会导致未覆盖的url返回null和错误。

以这种方式设置对我有用:
InMemoryWebApiModule.forRoot(InMemoryDataService, {passThruUnknownUrl: true})

关于javascript - 无法在Angular2中进行简单的http GET调用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40483800/

10-12 07:29
查看更多