0

I am trying to request a web page with a http call and harwest the data.

I could avoid the cross-origin with a chrome plug-in but still when I make the request, the response is always "null".

How could I fetch a html page inside my angular app as a json object ?

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

        this.results = data;
    });
}
5
  • 1
    Why do you expect data to be an object with a results attribute? What is the type of this.http? Commented Sep 8, 2017 at 8:56
  • my bad, corrected... Commented Sep 8, 2017 at 9:18
  • So now, what is null? where do you log/display the response to determine that it's null? And again, what is the type of this.http? Commented Sep 8, 2017 at 9:21
  • i m logging "this.results" somewhere else.. I didnt think that console.log part is that important :)) The response is null.. Here the data is my response.. http is from @angular/http, i m not sure i understood the question.. Commented Sep 8, 2017 at 9:26
  • It is important. Commented Sep 8, 2017 at 9:28

2 Answers 2

1

You should make sure the request URL ends with .json such as:

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

This will sort the mime type out automatically. Something like this:

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'];
});

You may also need to use data.json() in your promise resolution code.

Sign up to request clarification or add additional context in comments.

Comments

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);
  }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.