我想從我的Angular2應用做一個簡單的HTTP GET
要求:不能在Angular2
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
工作演示,瞭解
似乎並不與上面的代碼的問題。一個建議 - 使用'()=>'而不是'function()',因爲'function()'將導致'this.'不再指向當前的類實例。也許你已經在你發佈的代碼之外,這裏會導致錯誤。 –
我從localhost調用到heroku應用程序(https://myherokuapp.com)。這是相關的嗎? – Yuvals
@GünterZöchbauer這沒有幫助。非常奇怪的是我試圖調用一個返回JSON的工作服務,我得到:'狀態錯誤:404 Not Found for URL:null' – Yuvals