2017-09-08 10 views
0

http呼び出しでWebページをリクエストし、データを取得しようとしています。角4 jsonオブジェクトとしてWebページのコンテンツを要求する方法

私はクロームプラグインとのクロスオリジンを避けることができましたが、依然としてリクエストを行うと応答は常に「null」です。

角型アプリケーション内でhtmlページをjsonオブジェクトとして取得するにはどうすればよいですか?

ngOnInit(): void { 
    // Make the HTTP request: 
    this.http.get('http://www.hurriyet.com.tr/gundem/').subscribe(data => { 

     this.results = data; 
    }); 
} 
+1

あなたはデータが結果属性を持つオブジェクトであることを期待するのはなぜ? this.httpのタイプは何ですか? –

+0

私の悪い、訂正された... –

+0

だから、nullは何ですか?それがヌルであると判断するために応答を記録/表示する場所はどこですか?もう一度、このタイプのタイプは何ですか? –

答えて

1

あなたは、リクエストURLのような.jsonで終わることを確認する必要があります。これは、自動的に入力パントマイムをソートします

http://www.hurriyet.com.tr/gundem/gundem.json

。このようなもの:

this.http.get('http://www.hurriyet.com.tr/gundem/gundem.json').subscribe(data => { 
    // Read the result field from the JSON response. 
    this.results = data['results']; 
}); 

約束解消コードでもdata.json()を使用する必要があります。

0
import { Injectable } from '@angular/core'; 
import { Http, Response } from '@angular/http'; 
import 'rxjs/add/operator/toPromise'; 

@Injectable() 
export class SomeService { 

    constructor(private http: Http) {} 

    getItems() { 
     return this.http.get('http://www.hurriyet.com.tr/gundem/gundem.json') 
      .toPromise() 
      .then(res => <Type[]> res.json().data) 
      .then(data => { return data; }); 
    } 
} 

export interface Type { 
    id?; 
    title?; 
} 

app.component.ts

import {SomeService} from './some.service'; 
export class AppComponent implements OnInit { 

    constructor(private someService: SomeService) { 
    } 

    ngOnInit() { 
    this.someService.getItems().then(rows => this.rows = rows); 
    } 
} 
関連する問題